1cabdff1aSopenharmony_ci/*
2cabdff1aSopenharmony_ci * HQX DSP routines
3cabdff1aSopenharmony_ci *
4cabdff1aSopenharmony_ci * This file is part of FFmpeg.
5cabdff1aSopenharmony_ci *
6cabdff1aSopenharmony_ci * FFmpeg is free software; you can redistribute it and/or
7cabdff1aSopenharmony_ci * modify it under the terms of the GNU Lesser General Public
8cabdff1aSopenharmony_ci * License as published by the Free Software Foundation; either
9cabdff1aSopenharmony_ci * version 2.1 of the License, or (at your option) any later version.
10cabdff1aSopenharmony_ci *
11cabdff1aSopenharmony_ci * FFmpeg is distributed in the hope that it will be useful,
12cabdff1aSopenharmony_ci * but WITHOUT ANY WARRANTY; without even the implied warranty of
13cabdff1aSopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14cabdff1aSopenharmony_ci * Lesser General Public License for more details.
15cabdff1aSopenharmony_ci *
16cabdff1aSopenharmony_ci * You should have received a copy of the GNU Lesser General Public
17cabdff1aSopenharmony_ci * License along with FFmpeg; if not, write to the Free Software
18cabdff1aSopenharmony_ci * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19cabdff1aSopenharmony_ci */
20cabdff1aSopenharmony_ci
21cabdff1aSopenharmony_ci#include <stdint.h>
22cabdff1aSopenharmony_ci
23cabdff1aSopenharmony_ci#include "libavutil/common.h"
24cabdff1aSopenharmony_ci
25cabdff1aSopenharmony_ci#include "hqxdsp.h"
26cabdff1aSopenharmony_ci
27cabdff1aSopenharmony_cistatic inline void idct_col(int16_t *blk, const uint8_t *quant)
28cabdff1aSopenharmony_ci{
29cabdff1aSopenharmony_ci    int t0, t1, t2, t3, t4, t5, t6, t7, t8, t9, tA, tB, tC, tD, tE, tF;
30cabdff1aSopenharmony_ci    int t10, t11, t12, t13;
31cabdff1aSopenharmony_ci    int s0, s1, s2, s3, s4, s5, s6, s7;
32cabdff1aSopenharmony_ci
33cabdff1aSopenharmony_ci    s0 = (int) blk[0 * 8] * quant[0 * 8];
34cabdff1aSopenharmony_ci    s1 = (int) blk[1 * 8] * quant[1 * 8];
35cabdff1aSopenharmony_ci    s2 = (int) blk[2 * 8] * quant[2 * 8];
36cabdff1aSopenharmony_ci    s3 = (int) blk[3 * 8] * quant[3 * 8];
37cabdff1aSopenharmony_ci    s4 = (int) blk[4 * 8] * quant[4 * 8];
38cabdff1aSopenharmony_ci    s5 = (int) blk[5 * 8] * quant[5 * 8];
39cabdff1aSopenharmony_ci    s6 = (int) blk[6 * 8] * quant[6 * 8];
40cabdff1aSopenharmony_ci    s7 = (int) blk[7 * 8] * quant[7 * 8];
41cabdff1aSopenharmony_ci
42cabdff1aSopenharmony_ci    t0  =  (int)(s3 * 19266U + s5 * 12873U) >> 15;
43cabdff1aSopenharmony_ci    t1  =  (int)(s5 * 19266U - s3 * 12873U) >> 15;
44cabdff1aSopenharmony_ci    t2  = ((int)(s7 * 4520U  + s1 * 22725U) >> 15) - t0;
45cabdff1aSopenharmony_ci    t3  = ((int)(s1 * 4520U  - s7 * 22725U) >> 15) - t1;
46cabdff1aSopenharmony_ci    t4  = t0 * 2 + t2;
47cabdff1aSopenharmony_ci    t5  = t1 * 2 + t3;
48cabdff1aSopenharmony_ci    t6  = t2 - t3;
49cabdff1aSopenharmony_ci    t7  = t3 * 2 + t6;
50cabdff1aSopenharmony_ci    t8  = (int)(t6 * 11585U) >> 14;
51cabdff1aSopenharmony_ci    t9  = (int)(t7 * 11585U) >> 14;
52cabdff1aSopenharmony_ci    tA  = (int)(s2 * 8867U - s6 * 21407U) >> 14;
53cabdff1aSopenharmony_ci    tB  = (int)(s6 * 8867U + s2 * 21407U) >> 14;
54cabdff1aSopenharmony_ci    tC  = (s0 >> 1) - (s4 >> 1);
55cabdff1aSopenharmony_ci    tD  = (s4 >> 1) * 2 + tC;
56cabdff1aSopenharmony_ci    tE  = tC - (tA >> 1);
57cabdff1aSopenharmony_ci    tF  = tD - (tB >> 1);
58cabdff1aSopenharmony_ci    t10 = tF - t5;
59cabdff1aSopenharmony_ci    t11 = tE - t8;
60cabdff1aSopenharmony_ci    t12 = tE + (tA >> 1) * 2 - t9;
61cabdff1aSopenharmony_ci    t13 = tF + (tB >> 1) * 2 - t4;
62cabdff1aSopenharmony_ci
63cabdff1aSopenharmony_ci    blk[0 * 8] = t13 + t4 * 2;
64cabdff1aSopenharmony_ci    blk[1 * 8] = t12 + t9 * 2;
65cabdff1aSopenharmony_ci    blk[2 * 8] = t11 + t8 * 2;
66cabdff1aSopenharmony_ci    blk[3 * 8] = t10 + t5 * 2;
67cabdff1aSopenharmony_ci    blk[4 * 8] = t10;
68cabdff1aSopenharmony_ci    blk[5 * 8] = t11;
69cabdff1aSopenharmony_ci    blk[6 * 8] = t12;
70cabdff1aSopenharmony_ci    blk[7 * 8] = t13;
71cabdff1aSopenharmony_ci}
72cabdff1aSopenharmony_ci
73cabdff1aSopenharmony_cistatic inline void idct_row(int16_t *blk)
74cabdff1aSopenharmony_ci{
75cabdff1aSopenharmony_ci    int t0, t1, t2, t3, t4, t5, t6, t7, t8, t9, tA, tB, tC, tD, tE, tF;
76cabdff1aSopenharmony_ci    int t10, t11, t12, t13;
77cabdff1aSopenharmony_ci
78cabdff1aSopenharmony_ci    t0  =  (blk[3] * 19266 + blk[5] * 12873) >> 14;
79cabdff1aSopenharmony_ci    t1  =  (blk[5] * 19266 - blk[3] * 12873) >> 14;
80cabdff1aSopenharmony_ci    t2  = ((blk[7] * 4520  + blk[1] * 22725) >> 14) - t0;
81cabdff1aSopenharmony_ci    t3  = ((blk[1] * 4520  - blk[7] * 22725) >> 14) - t1;
82cabdff1aSopenharmony_ci    t4  = t0 * 2 + t2;
83cabdff1aSopenharmony_ci    t5  = t1 * 2 + t3;
84cabdff1aSopenharmony_ci    t6  = t2 - t3;
85cabdff1aSopenharmony_ci    t7  = t3 * 2 + t6;
86cabdff1aSopenharmony_ci    t8  = (t6 * 11585) >> 14;
87cabdff1aSopenharmony_ci    t9  = (t7 * 11585) >> 14;
88cabdff1aSopenharmony_ci    tA  = (blk[2] * 8867 - blk[6] * 21407) >> 14;
89cabdff1aSopenharmony_ci    tB  = (blk[6] * 8867 + blk[2] * 21407) >> 14;
90cabdff1aSopenharmony_ci    tC  = blk[0] - blk[4];
91cabdff1aSopenharmony_ci    tD  = blk[4] * 2 + tC;
92cabdff1aSopenharmony_ci    tE  = tC - tA;
93cabdff1aSopenharmony_ci    tF  = tD - tB;
94cabdff1aSopenharmony_ci    t10 = tF - t5;
95cabdff1aSopenharmony_ci    t11 = tE - t8;
96cabdff1aSopenharmony_ci    t12 = tE + tA * 2 - t9;
97cabdff1aSopenharmony_ci    t13 = tF + tB * 2 - t4;
98cabdff1aSopenharmony_ci
99cabdff1aSopenharmony_ci    blk[0] = (t13 + t4 * 2 + 4) >> 3;
100cabdff1aSopenharmony_ci    blk[1] = (t12 + t9 * 2 + 4) >> 3;
101cabdff1aSopenharmony_ci    blk[2] = (t11 + t8 * 2 + 4) >> 3;
102cabdff1aSopenharmony_ci    blk[3] = (t10 + t5 * 2 + 4) >> 3;
103cabdff1aSopenharmony_ci    blk[4] = (t10          + 4) >> 3;
104cabdff1aSopenharmony_ci    blk[5] = (t11          + 4) >> 3;
105cabdff1aSopenharmony_ci    blk[6] = (t12          + 4) >> 3;
106cabdff1aSopenharmony_ci    blk[7] = (t13          + 4) >> 3;
107cabdff1aSopenharmony_ci}
108cabdff1aSopenharmony_ci
109cabdff1aSopenharmony_cistatic void hqx_idct_put(uint16_t *dst, ptrdiff_t stride,
110cabdff1aSopenharmony_ci                         int16_t *block, const uint8_t *quant)
111cabdff1aSopenharmony_ci{
112cabdff1aSopenharmony_ci    int i, j;
113cabdff1aSopenharmony_ci
114cabdff1aSopenharmony_ci    for (i = 0; i < 8; i++)
115cabdff1aSopenharmony_ci        idct_col(block + i, quant + i);
116cabdff1aSopenharmony_ci    for (i = 0; i < 8; i++)
117cabdff1aSopenharmony_ci        idct_row(block + i * 8);
118cabdff1aSopenharmony_ci
119cabdff1aSopenharmony_ci    for (i = 0; i < 8; i++) {
120cabdff1aSopenharmony_ci        for (j = 0; j < 8; j++) {
121cabdff1aSopenharmony_ci            int v = av_clip_uintp2(block[j + i * 8] + 0x800, 12);
122cabdff1aSopenharmony_ci            dst[j] = (v << 4) | (v >> 8);
123cabdff1aSopenharmony_ci        }
124cabdff1aSopenharmony_ci        dst += stride >> 1;
125cabdff1aSopenharmony_ci    }
126cabdff1aSopenharmony_ci}
127cabdff1aSopenharmony_ci
128cabdff1aSopenharmony_ciav_cold void ff_hqxdsp_init(HQXDSPContext *c)
129cabdff1aSopenharmony_ci{
130cabdff1aSopenharmony_ci    c->idct_put = hqx_idct_put;
131cabdff1aSopenharmony_ci}
132