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 void ff_put_pixels_clamped_c(const int16_t *block, uint8_t *av_restrict pixels,
89 for (i = 0; i < 8; i++) {
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]);
94 pixels[4] = av_clip_uint8(block[4]);
95 pixels[5] = av_clip_uint8(block[5]);
96 pixels[6] = av_clip_uint8(block[6]);
97 pixels[7] = av_clip_uint8(block[7]);
104 static void put_pixels_clamped4_c(const int16_t *block, uint8_t *av_restrict pixels,
109 /* read the pixels */
111 pixels[0] = av_clip_uint8(block[0]);
112 pixels[1] = av_clip_uint8(block[1]);
113 pixels[2] = av_clip_uint8(block[2]);
114 pixels[3] = av_clip_uint8(block[3]);
121 static void put_pixels_clamped2_c(const int16_t *block, uint8_t *av_restrict pixels,
126 /* read the pixels */
128 pixels[0] = av_clip_uint8(block[0]);
129 pixels[1] = av_clip_uint8(block[1]);
136 static void put_signed_pixels_clamped_c(const int16_t *block,
137 uint8_t *av_restrict pixels,
142 for (i = 0; i < 8; i++) {
143 for (j = 0; j < 8; j++) {
146 else if (*block > 127)
149 *pixels = (uint8_t) (*block + 128);
153 pixels += (line_size - 8);
157 void ff_add_pixels_clamped_c(const int16_t *block, uint8_t *av_restrict pixels,
162 /* read the pixels */
163 for (i = 0; i < 8; i++) {
164 pixels[0] = av_clip_uint8(pixels[0] + block[0]);
165 pixels[1] = av_clip_uint8(pixels[1] + block[1]);
166 pixels[2] = av_clip_uint8(pixels[2] + block[2]);
167 pixels[3] = av_clip_uint8(pixels[3] + block[3]);
168 pixels[4] = av_clip_uint8(pixels[4] + block[4]);
169 pixels[5] = av_clip_uint8(pixels[5] + block[5]);
170 pixels[6] = av_clip_uint8(pixels[6] + block[6]);
171 pixels[7] = av_clip_uint8(pixels[7] + block[7]);
177 static void add_pixels_clamped4_c(const int16_t *block, uint8_t *av_restrict pixels,
182 /* read the pixels */
184 pixels[0] = av_clip_uint8(pixels[0] + block[0]);
185 pixels[1] = av_clip_uint8(pixels[1] + block[1]);
186 pixels[2] = av_clip_uint8(pixels[2] + block[2]);
187 pixels[3] = av_clip_uint8(pixels[3] + block[3]);
193 static void add_pixels_clamped2_c(const int16_t *block, uint8_t *av_restrict pixels,
198 /* read the pixels */
200 pixels[0] = av_clip_uint8(pixels[0] + block[0]);
201 pixels[1] = av_clip_uint8(pixels[1] + block[1]);
207 static void ff_jref_idct4_put(uint8_t *dest, ptrdiff_t line_size, int16_t *block)
209 ff_j_rev_dct4 (block);
210 put_pixels_clamped4_c(block, dest, line_size);
212 static void ff_jref_idct4_add(uint8_t *dest, ptrdiff_t line_size, int16_t *block)
214 ff_j_rev_dct4 (block);
215 add_pixels_clamped4_c(block, dest, line_size);
218 static void ff_jref_idct2_put(uint8_t *dest, ptrdiff_t line_size, int16_t *block)
220 ff_j_rev_dct2 (block);
221 put_pixels_clamped2_c(block, dest, line_size);
223 static void ff_jref_idct2_add(uint8_t *dest, ptrdiff_t line_size, int16_t *block)
225 ff_j_rev_dct2 (block);
226 add_pixels_clamped2_c(block, dest, line_size);
229 static void ff_jref_idct1_put(uint8_t *dest, ptrdiff_t line_size, int16_t *block)
231 dest[0] = av_clip_uint8((block[0] + 4)>>3);
233 static void ff_jref_idct1_add(uint8_t *dest, ptrdiff_t line_size, int16_t *block)
235 dest[0] = av_clip_uint8(dest[0] + ((block[0] + 4)>>3));
238 av_cold void ff_idctdsp_init(IDCTDSPContext *c, AVCodecContext *avctx)
240 const unsigned high_bit_depth = avctx->bits_per_raw_sample > 8;
242 if (avctx->lowres==1) {
243 c->idct_put = ff_jref_idct4_put;
244 c->idct_add = ff_jref_idct4_add;
245 c->idct = ff_j_rev_dct4;
246 c->perm_type = FF_IDCT_PERM_NONE;
247 } else if (avctx->lowres==2) {
248 c->idct_put = ff_jref_idct2_put;
249 c->idct_add = ff_jref_idct2_add;
250 c->idct = ff_j_rev_dct2;
251 c->perm_type = FF_IDCT_PERM_NONE;
252 } else if (avctx->lowres==3) {
253 c->idct_put = ff_jref_idct1_put;
254 c->idct_add = ff_jref_idct1_add;
255 c->idct = ff_j_rev_dct1;
256 c->perm_type = FF_IDCT_PERM_NONE;
258 if (avctx->bits_per_raw_sample == 10 || avctx->bits_per_raw_sample == 9) {
259 /* 10-bit MPEG-4 Simple Studio Profile requires a higher precision IDCT
260 However, it only uses idct_put */
261 if (c->mpeg4_studio_profile) {
262 c->idct_put = ff_simple_idct_put_int32_10bit;
266 c->idct_put = ff_simple_idct_put_int16_10bit;
267 c->idct_add = ff_simple_idct_add_int16_10bit;
268 c->idct = ff_simple_idct_int16_10bit;
270 c->perm_type = FF_IDCT_PERM_NONE;
271 } else if (avctx->bits_per_raw_sample == 12) {
272 c->idct_put = ff_simple_idct_put_int16_12bit;
273 c->idct_add = ff_simple_idct_add_int16_12bit;
274 c->idct = ff_simple_idct_int16_12bit;
275 c->perm_type = FF_IDCT_PERM_NONE;
277 if (avctx->idct_algo == FF_IDCT_INT) {
278 c->idct_put = ff_jref_idct_put;
279 c->idct_add = ff_jref_idct_add;
280 c->idct = ff_j_rev_dct;
281 c->perm_type = FF_IDCT_PERM_LIBMPEG2;
283 } else if (avctx->idct_algo == FF_IDCT_FAAN) {
284 c->idct_put = ff_faanidct_put;
285 c->idct_add = ff_faanidct_add;
286 c->idct = ff_faanidct;
287 c->perm_type = FF_IDCT_PERM_NONE;
288 #endif /* CONFIG_FAANIDCT */
289 } else { // accurate/default
290 /* Be sure FF_IDCT_NONE will select this one, since it uses FF_IDCT_PERM_NONE */
291 c->idct_put = ff_simple_idct_put_int16_8bit;
292 c->idct_add = ff_simple_idct_add_int16_8bit;
293 c->idct = ff_simple_idct_int16_8bit;
294 c->perm_type = FF_IDCT_PERM_NONE;
299 c->put_pixels_clamped = ff_put_pixels_clamped_c;
300 c->put_signed_pixels_clamped = put_signed_pixels_clamped_c;
301 c->add_pixels_clamped = ff_add_pixels_clamped_c;
303 if (CONFIG_MPEG4_DECODER && avctx->idct_algo == FF_IDCT_XVID)
304 ff_xvid_idct_init(c, avctx);
307 ff_idctdsp_init_aarch64(c, avctx, high_bit_depth);
309 ff_idctdsp_init_alpha(c, avctx, high_bit_depth);
311 ff_idctdsp_init_arm(c, avctx, high_bit_depth);
313 ff_idctdsp_init_ppc(c, avctx, high_bit_depth);
315 ff_idctdsp_init_x86(c, avctx, high_bit_depth);
317 ff_idctdsp_init_mips(c, avctx, high_bit_depth);
319 ff_init_scantable_permutation(c->idct_permutation,