libfdk-aacdec: Decode the first AAC frame to reliably identify the bitstream
[ffmpeg.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                                            int idct_permutation_type)
51 {
52     int i;
53
54     if (ARCH_X86)
55         if (ff_init_scantable_permutation_x86(idct_permutation,
56                                               idct_permutation_type))
57             return;
58
59     switch (idct_permutation_type) {
60     case FF_NO_IDCT_PERM:
61         for (i = 0; i < 64; i++)
62             idct_permutation[i] = i;
63         break;
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);
67         break;
68     case FF_TRANSPOSE_IDCT_PERM:
69         for (i = 0; i < 64; i++)
70             idct_permutation[i] = ((i & 7) << 3) | (i >> 3);
71         break;
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);
75         break;
76     default:
77         av_log(NULL, AV_LOG_ERROR,
78                "Internal error, IDCT permutation not set\n");
79     }
80 }
81
82 static void put_pixels_clamped_c(const int16_t *block, uint8_t *restrict pixels,
83                                  int line_size)
84 {
85     int i;
86
87     /* read the 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]);
97
98         pixels += line_size;
99         block  += 8;
100     }
101 }
102
103 static void put_signed_pixels_clamped_c(const int16_t *block,
104                                         uint8_t *restrict pixels,
105                                         int line_size)
106 {
107     int i, j;
108
109     for (i = 0; i < 8; i++) {
110         for (j = 0; j < 8; j++) {
111             if (*block < -128)
112                 *pixels = 0;
113             else if (*block > 127)
114                 *pixels = 255;
115             else
116                 *pixels = (uint8_t) (*block + 128);
117             block++;
118             pixels++;
119         }
120         pixels += (line_size - 8);
121     }
122 }
123
124 static void add_pixels_clamped_c(const int16_t *block, uint8_t *restrict pixels,
125                                  int line_size)
126 {
127     int i;
128
129     /* read the pixels */
130     for (i = 0; i < 8; i++) {
131         pixels[0] = av_clip_uint8(pixels[0] + block[0]);
132         pixels[1] = av_clip_uint8(pixels[1] + block[1]);
133         pixels[2] = av_clip_uint8(pixels[2] + block[2]);
134         pixels[3] = av_clip_uint8(pixels[3] + block[3]);
135         pixels[4] = av_clip_uint8(pixels[4] + block[4]);
136         pixels[5] = av_clip_uint8(pixels[5] + block[5]);
137         pixels[6] = av_clip_uint8(pixels[6] + block[6]);
138         pixels[7] = av_clip_uint8(pixels[7] + block[7]);
139         pixels   += line_size;
140         block    += 8;
141     }
142 }
143
144 static void jref_idct_put(uint8_t *dest, int line_size, int16_t *block)
145 {
146     ff_j_rev_dct(block);
147     put_pixels_clamped_c(block, dest, line_size);
148 }
149
150 static void jref_idct_add(uint8_t *dest, int line_size, int16_t *block)
151 {
152     ff_j_rev_dct(block);
153     add_pixels_clamped_c(block, dest, line_size);
154 }
155
156 av_cold void ff_idctdsp_init(IDCTDSPContext *c, AVCodecContext *avctx)
157 {
158     const unsigned high_bit_depth = avctx->bits_per_raw_sample > 8;
159
160     if (avctx->bits_per_raw_sample == 10) {
161         c->idct_put              = ff_simple_idct_put_10;
162         c->idct_add              = ff_simple_idct_add_10;
163         c->idct                  = ff_simple_idct_10;
164         c->idct_permutation_type = FF_NO_IDCT_PERM;
165     } else {
166         if (avctx->idct_algo == FF_IDCT_INT) {
167             c->idct_put              = jref_idct_put;
168             c->idct_add              = jref_idct_add;
169             c->idct                  = ff_j_rev_dct;
170             c->idct_permutation_type = FF_LIBMPEG2_IDCT_PERM;
171         } else if (avctx->idct_algo == FF_IDCT_FAAN) {
172             c->idct_put              = ff_faanidct_put;
173             c->idct_add              = ff_faanidct_add;
174             c->idct                  = ff_faanidct;
175             c->idct_permutation_type = FF_NO_IDCT_PERM;
176         } else { // accurate/default
177             c->idct_put              = ff_simple_idct_put_8;
178             c->idct_add              = ff_simple_idct_add_8;
179             c->idct                  = ff_simple_idct_8;
180             c->idct_permutation_type = FF_NO_IDCT_PERM;
181         }
182     }
183
184     c->put_pixels_clamped        = put_pixels_clamped_c;
185     c->put_signed_pixels_clamped = put_signed_pixels_clamped_c;
186     c->add_pixels_clamped        = add_pixels_clamped_c;
187
188     if (ARCH_ARM)
189         ff_idctdsp_init_arm(c, avctx, high_bit_depth);
190     if (ARCH_PPC)
191         ff_idctdsp_init_ppc(c, avctx, high_bit_depth);
192     if (ARCH_X86)
193         ff_idctdsp_init_x86(c, avctx, high_bit_depth);
194
195     ff_init_scantable_permutation(c->idct_permutation,
196                                   c->idct_permutation_type);
197 }