2 * This file is part of FFmpeg.
4 * FFmpeg 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.
9 * FFmpeg 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.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with FFmpeg; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 #include "libavutil/attributes.h"
21 #include "libavutil/common.h"
26 #include "simple_idct.h"
29 av_cold void ff_init_scantable(uint8_t *permutation, ScanTable *st,
30 const uint8_t *src_scantable)
34 st->scantable = src_scantable;
36 for (i = 0; i < 64; i++) {
37 int j = src_scantable[i];
38 st->permutated[i] = permutation[j];
42 for (i = 0; i < 64; i++) {
43 int j = st->permutated[i];
46 st->raster_end[i] = end;
50 av_cold void ff_init_scantable_permutation(uint8_t *idct_permutation,
51 enum idct_permutation_type perm_type)
56 if (ff_init_scantable_permutation_x86(idct_permutation,
61 case FF_IDCT_PERM_NONE:
62 for (i = 0; i < 64; i++)
63 idct_permutation[i] = i;
65 case FF_IDCT_PERM_LIBMPEG2:
66 for (i = 0; i < 64; i++)
67 idct_permutation[i] = (i & 0x38) | ((i & 6) >> 1) | ((i & 1) << 2);
69 case FF_IDCT_PERM_TRANSPOSE:
70 for (i = 0; i < 64; i++)
71 idct_permutation[i] = ((i & 7) << 3) | (i >> 3);
73 case FF_IDCT_PERM_PARTTRANS:
74 for (i = 0; i < 64; i++)
75 idct_permutation[i] = (i & 0x24) | ((i & 3) << 3) | ((i >> 3) & 3);
78 av_log(NULL, AV_LOG_ERROR,
79 "Internal error, IDCT permutation not set\n");
83 static void put_pixels_clamped4_c(const int16_t *block, uint8_t *av_restrict pixels,
90 pixels[0] = av_clip_uint8(block[0]);
91 pixels[1] = av_clip_uint8(block[1]);
92 pixels[2] = av_clip_uint8(block[2]);
93 pixels[3] = av_clip_uint8(block[3]);
100 static void put_pixels_clamped2_c(const int16_t *block, uint8_t *av_restrict pixels,
105 /* read the pixels */
107 pixels[0] = av_clip_uint8(block[0]);
108 pixels[1] = av_clip_uint8(block[1]);
115 static void put_signed_pixels_clamped_c(const int16_t *block,
116 uint8_t *av_restrict pixels,
121 for (i = 0; i < 8; i++) {
122 for (j = 0; j < 8; j++) {
125 else if (*block > 127)
128 *pixels = (uint8_t) (*block + 128);
132 pixels += (line_size - 8);
136 static void add_pixels_clamped4_c(const int16_t *block, uint8_t *av_restrict pixels,
141 /* read the pixels */
143 pixels[0] = av_clip_uint8(pixels[0] + block[0]);
144 pixels[1] = av_clip_uint8(pixels[1] + block[1]);
145 pixels[2] = av_clip_uint8(pixels[2] + block[2]);
146 pixels[3] = av_clip_uint8(pixels[3] + block[3]);
152 static void add_pixels_clamped2_c(const int16_t *block, uint8_t *av_restrict pixels,
157 /* read the pixels */
159 pixels[0] = av_clip_uint8(pixels[0] + block[0]);
160 pixels[1] = av_clip_uint8(pixels[1] + block[1]);
166 static void jref_idct_put(uint8_t *dest, int line_size, int16_t *block)
169 put_pixels_clamped_c(block, dest, line_size);
172 static void jref_idct_add(uint8_t *dest, int line_size, int16_t *block)
175 add_pixels_clamped_c(block, dest, line_size);
177 static void ff_jref_idct4_put(uint8_t *dest, int line_size, int16_t *block)
179 ff_j_rev_dct4 (block);
180 put_pixels_clamped4_c(block, dest, line_size);
182 static void ff_jref_idct4_add(uint8_t *dest, int line_size, int16_t *block)
184 ff_j_rev_dct4 (block);
185 add_pixels_clamped4_c(block, dest, line_size);
188 static void ff_jref_idct2_put(uint8_t *dest, int line_size, int16_t *block)
190 ff_j_rev_dct2 (block);
191 put_pixels_clamped2_c(block, dest, line_size);
193 static void ff_jref_idct2_add(uint8_t *dest, int line_size, int16_t *block)
195 ff_j_rev_dct2 (block);
196 add_pixels_clamped2_c(block, dest, line_size);
199 static void ff_jref_idct1_put(uint8_t *dest, int line_size, int16_t *block)
201 dest[0] = av_clip_uint8((block[0] + 4)>>3);
203 static void ff_jref_idct1_add(uint8_t *dest, int line_size, int16_t *block)
205 dest[0] = av_clip_uint8(dest[0] + ((block[0] + 4)>>3));
208 av_cold void ff_idctdsp_init(IDCTDSPContext *c, AVCodecContext *avctx)
210 const unsigned high_bit_depth = avctx->bits_per_raw_sample > 8;
212 if (avctx->lowres==1) {
213 c->idct_put = ff_jref_idct4_put;
214 c->idct_add = ff_jref_idct4_add;
215 c->idct = ff_j_rev_dct4;
216 c->perm_type = FF_IDCT_PERM_NONE;
217 } else if (avctx->lowres==2) {
218 c->idct_put = ff_jref_idct2_put;
219 c->idct_add = ff_jref_idct2_add;
220 c->idct = ff_j_rev_dct2;
221 c->perm_type = FF_IDCT_PERM_NONE;
222 } else if (avctx->lowres==3) {
223 c->idct_put = ff_jref_idct1_put;
224 c->idct_add = ff_jref_idct1_add;
225 c->idct = ff_j_rev_dct1;
226 c->perm_type = FF_IDCT_PERM_NONE;
228 if (avctx->bits_per_raw_sample == 10) {
229 c->idct_put = ff_simple_idct_put_10;
230 c->idct_add = ff_simple_idct_add_10;
231 c->idct = ff_simple_idct_10;
232 c->perm_type = FF_IDCT_PERM_NONE;
233 } else if (avctx->bits_per_raw_sample == 12) {
234 c->idct_put = ff_simple_idct_put_12;
235 c->idct_add = ff_simple_idct_add_12;
236 c->idct = ff_simple_idct_12;
237 c->perm_type = FF_IDCT_PERM_NONE;
239 if (avctx->idct_algo == FF_IDCT_INT) {
240 c->idct_put = jref_idct_put;
241 c->idct_add = jref_idct_add;
242 c->idct = ff_j_rev_dct;
243 c->perm_type = FF_IDCT_PERM_LIBMPEG2;
244 } else if (avctx->idct_algo == FF_IDCT_FAAN) {
245 c->idct_put = ff_faanidct_put;
246 c->idct_add = ff_faanidct_add;
247 c->idct = ff_faanidct;
248 c->perm_type = FF_IDCT_PERM_NONE;
249 } else { // accurate/default
250 c->idct_put = ff_simple_idct_put_8;
251 c->idct_add = ff_simple_idct_add_8;
252 c->idct = ff_simple_idct_8;
253 c->perm_type = FF_IDCT_PERM_NONE;
258 c->put_pixels_clamped = put_pixels_clamped_c;
259 c->put_signed_pixels_clamped = put_signed_pixels_clamped_c;
260 c->add_pixels_clamped = add_pixels_clamped_c;
262 if (CONFIG_MPEG4_DECODER && avctx->idct_algo == FF_IDCT_XVID)
263 ff_xvididct_init(c, avctx);
266 ff_idctdsp_init_alpha(c, avctx, high_bit_depth);
268 ff_idctdsp_init_arm(c, avctx, high_bit_depth);
270 ff_idctdsp_init_ppc(c, avctx, high_bit_depth);
272 ff_idctdsp_init_x86(c, avctx, high_bit_depth);
274 ff_init_scantable_permutation(c->idct_permutation,