2 * Canopus HQ/HQA decoder
4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 #include "libavutil/attributes.h"
24 #include "libavutil/common.h"
26 #include "hq_hqadsp.h"
28 #define FIX_1_082 17734
29 #define FIX_1_847 30274
30 #define FIX_1_414 23170
31 #define FIX_2_613 21407 // divided by two to fit the range
33 #define IDCTMUL(a, b) ((int)((a) * (unsigned)(b)) >> 16)
35 static inline void idct_row(int16_t *blk)
37 int tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmpA;
38 int tmpB, tmpC, tmpD, tmpE, tmpF, tmp10, tmp11, tmp12, tmp13, tmp14;
40 tmp0 = blk[5] - blk[3];
41 tmp1 = blk[5] + blk[3];
42 tmp2 = blk[1] - blk[7];
43 tmp3 = blk[1] + blk[7];
45 tmp5 = IDCTMUL(tmp0 + tmp2, FIX_1_847);
46 tmp6 = IDCTMUL(tmp2, FIX_1_082) - tmp5;
47 tmp7 = tmp5 - IDCTMUL(tmp0, FIX_2_613) * 2;
49 tmp9 = tmp7 * 4 - tmp8;
50 tmpA = IDCTMUL(tmp4, FIX_1_414) * 4 - tmp9;
51 tmpB = tmp6 * 4 + tmpA;
52 tmpC = blk[2] + blk[6];
53 tmpD = blk[2] - blk[6];
54 tmpE = blk[0] - blk[4];
55 tmpF = blk[0] + blk[4];
57 tmp10 = IDCTMUL(tmpD, FIX_1_414) * 4 - tmpC;
63 blk[0] = tmp14 + tmp8;
64 blk[1] = tmp13 + tmp9;
65 blk[2] = tmp11 + tmpA;
66 blk[3] = tmp12 - tmpB;
67 blk[4] = tmp12 + tmpB;
68 blk[5] = tmp11 - tmpA;
69 blk[6] = tmp13 - tmp9;
70 blk[7] = tmp14 - tmp8;
73 static inline void idct_col(int16_t *blk)
75 int tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmpA;
76 int tmpB, tmpC, tmpD, tmpE, tmpF, tmp10, tmp11, tmp12, tmp13, tmp14;
78 tmp0 = blk[5 * 8] - blk[3 * 8];
79 tmp1 = blk[5 * 8] + blk[3 * 8];
80 tmp2 = blk[1 * 8] * 2 - (blk[7 * 8] >> 2);
81 tmp3 = blk[1 * 8] * 2 + (blk[7 * 8] >> 2);
83 tmp5 = IDCTMUL(tmp0 + tmp2, FIX_1_847);
84 tmp6 = IDCTMUL(tmp2, FIX_1_082) - tmp5;
85 tmp7 = tmp5 - IDCTMUL(tmp0, FIX_2_613) * 2;
86 tmp8 = (tmp3 + tmp1) >> 1;
87 tmp9 = tmp7 * 2 - tmp8;
88 tmpA = IDCTMUL(tmp4, FIX_1_414) * 2 - tmp9;
89 tmpB = tmp6 * 2 + tmpA;
90 tmpC = blk[2 * 8] + (blk[6 * 8] >> 1) >> 1;
91 tmpD = blk[2 * 8] - (blk[6 * 8] >> 1);
92 tmpE = (blk[0 * 8] >> 1) - (blk[4 * 8] >> 1) + 0x2020;
93 tmpF = (blk[0 * 8] >> 1) + (blk[4 * 8] >> 1) + 0x2020;
95 tmp10 = IDCTMUL(tmpD, FIX_1_414) * 2 - tmpC;
101 blk[0 * 8] = (tmp14 + tmp8) >> 6;
102 blk[1 * 8] = (tmp13 + tmp9) >> 6;
103 blk[2 * 8] = (tmp11 + tmpA) >> 6;
104 blk[3 * 8] = (tmp12 - tmpB) >> 6;
105 blk[4 * 8] = (tmp12 + tmpB) >> 6;
106 blk[5 * 8] = (tmp11 - tmpA) >> 6;
107 blk[6 * 8] = (tmp13 - tmp9) >> 6;
108 blk[7 * 8] = (tmp14 - tmp8) >> 6;
111 static void hq_idct_put(uint8_t *dst, int stride, int16_t *block)
115 for (i = 0; i < 8; i++)
116 idct_row(block + i * 8);
117 for (i = 0; i < 8; i++)
120 for (i = 0; i < 8; i++) {
121 for (j = 0; j < 8; j++)
122 dst[j] = av_clip_uint8(block[j + i * 8]);
127 av_cold void ff_hqdsp_init(HQDSPContext *c)
129 c->idct_put = hq_idct_put;