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"
28 av_cold void ff_init_scantable(uint8_t *permutation, ScanTable *st,
29 const uint8_t *src_scantable)
33 st->scantable = src_scantable;
35 for (i = 0; i < 64; i++) {
36 int j = src_scantable[i];
37 st->permutated[i] = permutation[j];
41 for (i = 0; i < 64; i++) {
42 int j = st->permutated[i];
45 st->raster_end[i] = end;
49 av_cold void ff_init_scantable_permutation(uint8_t *idct_permutation,
50 int idct_permutation_type)
55 if (ff_init_scantable_permutation_x86(idct_permutation,
56 idct_permutation_type))
59 switch (idct_permutation_type) {
61 for (i = 0; i < 64; i++)
62 idct_permutation[i] = i;
64 case FF_LIBMPEG2_IDCT_PERM:
65 for (i = 0; i < 64; i++)
66 idct_permutation[i] = (i & 0x38) | ((i & 6) >> 1) | ((i & 1) << 2);
68 case FF_TRANSPOSE_IDCT_PERM:
69 for (i = 0; i < 64; i++)
70 idct_permutation[i] = ((i & 7) << 3) | (i >> 3);
72 case FF_PARTTRANS_IDCT_PERM:
73 for (i = 0; i < 64; i++)
74 idct_permutation[i] = (i & 0x24) | ((i & 3) << 3) | ((i >> 3) & 3);
77 av_log(NULL, AV_LOG_ERROR,
78 "Internal error, IDCT permutation not set\n");
82 static void put_pixels_clamped_c(const int16_t *block, uint8_t *av_restrict pixels,
88 for (i = 0; i < 8; i++) {
89 pixels[0] = av_clip_uint8(block[0]);
90 pixels[1] = av_clip_uint8(block[1]);
91 pixels[2] = av_clip_uint8(block[2]);
92 pixels[3] = av_clip_uint8(block[3]);
93 pixels[4] = av_clip_uint8(block[4]);
94 pixels[5] = av_clip_uint8(block[5]);
95 pixels[6] = av_clip_uint8(block[6]);
96 pixels[7] = av_clip_uint8(block[7]);
103 static void put_pixels_clamped4_c(const int16_t *block, uint8_t *av_restrict pixels,
108 /* read the pixels */
110 pixels[0] = av_clip_uint8(block[0]);
111 pixels[1] = av_clip_uint8(block[1]);
112 pixels[2] = av_clip_uint8(block[2]);
113 pixels[3] = av_clip_uint8(block[3]);
120 static void put_pixels_clamped2_c(const int16_t *block, uint8_t *av_restrict pixels,
125 /* read the pixels */
127 pixels[0] = av_clip_uint8(block[0]);
128 pixels[1] = av_clip_uint8(block[1]);
135 static void put_signed_pixels_clamped_c(const int16_t *block,
136 uint8_t *av_restrict pixels,
141 for (i = 0; i < 8; i++) {
142 for (j = 0; j < 8; j++) {
145 else if (*block > 127)
148 *pixels = (uint8_t) (*block + 128);
152 pixels += (line_size - 8);
156 static void add_pixels_clamped_c(const int16_t *block, uint8_t *av_restrict pixels,
161 /* read the pixels */
162 for (i = 0; i < 8; i++) {
163 pixels[0] = av_clip_uint8(pixels[0] + block[0]);
164 pixels[1] = av_clip_uint8(pixels[1] + block[1]);
165 pixels[2] = av_clip_uint8(pixels[2] + block[2]);
166 pixels[3] = av_clip_uint8(pixels[3] + block[3]);
167 pixels[4] = av_clip_uint8(pixels[4] + block[4]);
168 pixels[5] = av_clip_uint8(pixels[5] + block[5]);
169 pixels[6] = av_clip_uint8(pixels[6] + block[6]);
170 pixels[7] = av_clip_uint8(pixels[7] + block[7]);
176 static void add_pixels_clamped4_c(const int16_t *block, uint8_t *av_restrict pixels,
181 /* read the pixels */
183 pixels[0] = av_clip_uint8(pixels[0] + block[0]);
184 pixels[1] = av_clip_uint8(pixels[1] + block[1]);
185 pixels[2] = av_clip_uint8(pixels[2] + block[2]);
186 pixels[3] = av_clip_uint8(pixels[3] + block[3]);
192 static void add_pixels_clamped2_c(const int16_t *block, uint8_t *av_restrict pixels,
197 /* read the pixels */
199 pixels[0] = av_clip_uint8(pixels[0] + block[0]);
200 pixels[1] = av_clip_uint8(pixels[1] + block[1]);
206 static void jref_idct_put(uint8_t *dest, int line_size, int16_t *block)
209 put_pixels_clamped_c(block, dest, line_size);
212 static void jref_idct_add(uint8_t *dest, int line_size, int16_t *block)
215 add_pixels_clamped_c(block, dest, line_size);
217 static void ff_jref_idct4_put(uint8_t *dest, int line_size, int16_t *block)
219 ff_j_rev_dct4 (block);
220 put_pixels_clamped4_c(block, dest, line_size);
222 static void ff_jref_idct4_add(uint8_t *dest, int line_size, int16_t *block)
224 ff_j_rev_dct4 (block);
225 add_pixels_clamped4_c(block, dest, line_size);
228 static void ff_jref_idct2_put(uint8_t *dest, int line_size, int16_t *block)
230 ff_j_rev_dct2 (block);
231 put_pixels_clamped2_c(block, dest, line_size);
233 static void ff_jref_idct2_add(uint8_t *dest, int line_size, int16_t *block)
235 ff_j_rev_dct2 (block);
236 add_pixels_clamped2_c(block, dest, line_size);
239 static void ff_jref_idct1_put(uint8_t *dest, int line_size, int16_t *block)
241 dest[0] = av_clip_uint8((block[0] + 4)>>3);
243 static void ff_jref_idct1_add(uint8_t *dest, int line_size, int16_t *block)
245 dest[0] = av_clip_uint8(dest[0] + ((block[0] + 4)>>3));
248 av_cold void ff_idctdsp_init(IDCTDSPContext *c, AVCodecContext *avctx)
250 const unsigned high_bit_depth = avctx->bits_per_raw_sample > 8;
252 if (avctx->lowres==1) {
253 c->idct_put = ff_jref_idct4_put;
254 c->idct_add = ff_jref_idct4_add;
255 c->idct = ff_j_rev_dct4;
256 c->idct_permutation_type = FF_NO_IDCT_PERM;
257 } else if (avctx->lowres==2) {
258 c->idct_put = ff_jref_idct2_put;
259 c->idct_add = ff_jref_idct2_add;
260 c->idct = ff_j_rev_dct2;
261 c->idct_permutation_type = FF_NO_IDCT_PERM;
262 } else if (avctx->lowres==3) {
263 c->idct_put = ff_jref_idct1_put;
264 c->idct_add = ff_jref_idct1_add;
265 c->idct = ff_j_rev_dct1;
266 c->idct_permutation_type = FF_NO_IDCT_PERM;
268 if (avctx->bits_per_raw_sample == 10) {
269 c->idct_put = ff_simple_idct_put_10;
270 c->idct_add = ff_simple_idct_add_10;
271 c->idct = ff_simple_idct_10;
272 c->idct_permutation_type = FF_NO_IDCT_PERM;
273 } else if (avctx->bits_per_raw_sample == 12) {
274 c->idct_put = ff_simple_idct_put_12;
275 c->idct_add = ff_simple_idct_add_12;
276 c->idct = ff_simple_idct_12;
277 c->idct_permutation_type = FF_NO_IDCT_PERM;
279 if (avctx->idct_algo == FF_IDCT_INT) {
280 c->idct_put = jref_idct_put;
281 c->idct_add = jref_idct_add;
282 c->idct = ff_j_rev_dct;
283 c->idct_permutation_type = FF_LIBMPEG2_IDCT_PERM;
284 } else if (avctx->idct_algo == FF_IDCT_FAAN) {
285 c->idct_put = ff_faanidct_put;
286 c->idct_add = ff_faanidct_add;
287 c->idct = ff_faanidct;
288 c->idct_permutation_type = FF_NO_IDCT_PERM;
289 } else { // accurate/default
290 c->idct_put = ff_simple_idct_put_8;
291 c->idct_add = ff_simple_idct_add_8;
292 c->idct = ff_simple_idct_8;
293 c->idct_permutation_type = FF_NO_IDCT_PERM;
298 c->put_pixels_clamped = put_pixels_clamped_c;
299 c->put_signed_pixels_clamped = put_signed_pixels_clamped_c;
300 c->add_pixels_clamped = add_pixels_clamped_c;
303 ff_idctdsp_init_arm(c, avctx, high_bit_depth);
305 ff_idctdsp_init_ppc(c, avctx, high_bit_depth);
307 ff_idctdsp_init_x86(c, avctx, high_bit_depth);
309 ff_init_scantable_permutation(c->idct_permutation,
310 c->idct_permutation_type);