History

lavu/tx: add DCT-II implementation

Lynne committed 3 years ago

Commit 504b7be1 parent 93c30bd2 files · +122 −0Browse files

Unified · Split

2 files
  1. libavutil/tx.h +14 −0
  2. libavutil/tx_template.c +108 −0
libavutil/tx.h+14−0
@@ -91,6 +91,20 @@ enum AVTXType { · 90 unmodified lines
9191 AV_TX_DOUBLE_RDFT = 7,
9292 AV_TX_INT32_RDFT = 8,
9393
94+ /**
95+ * Real to real (DCT) transforms.
96+ *
97+ * The forward transform is a DCT-II.
98+ * The inverse transform is a DCT-III.
99+ *
100+ * The input array is always overwritten. DCT-III requires that the
101+ * input be padded with 2 extra samples. Stride must be set to the
102+ * spacing between two samples in bytes.
103+ */
104+ AV_TX_FLOAT_DCT = 9,
105+ AV_TX_DOUBLE_DCT = 10,
106+ AV_TX_INT32_DCT = 11,
107+
94108 /* Not part of the API, do not use */
95109 AV_TX_NB,
96110 };
libavutil/tx_template.c+108−0
@@ -1725,6 +1725,113 @@ static const FFTXCodelet TX_NAME(ff_tx_rdft_c2r_def) = { · 1724 unmodified lines
17251725 .prio = FF_TX_PRIO_BASE,
17261726 };
17271727
1728+static av_cold int TX_NAME(ff_tx_dct_init)(AVTXContext *s,
1729+ const FFTXCodelet *cd,
1730+ uint64_t flags,
1731+ FFTXCodeletOptions *opts,
1732+ int len, int inv,
1733+ const void *scale)
1734+{
1735+ int ret;
1736+ double freq;
1737+ TXSample *tab;
1738+ SCALE_TYPE rsc = *((SCALE_TYPE *)scale);
1739+
1740+
1741+ if ((ret = ff_tx_init_subtx(s, TX_TYPE(RDFT), flags, NULL, len, inv, &rsc)))
1742+ return ret;
1743+
1744+ s->exp = av_malloc((len/2)*3*sizeof(TXSample));
1745+ if (!s->exp)
1746+ return AVERROR(ENOMEM);
1747+
1748+ tab = (TXSample *)s->exp;
1749+
1750+ freq = M_PI/(len*2);
1751+
1752+ for (int i = 0; i < len; i++)
1753+ tab[i] = RESCALE(cos(i*freq)*(!inv + 1));
1754+
1755+ for (int i = 0; i < len/2; i++)
1756+ tab[len + i] = RESCALE(cos((len - 2*i - 1)*freq));
1757+
1758+ return 0;
1759+}
1760+
1761+static void TX_NAME(ff_tx_dctII)(AVTXContext *s, void *_dst,
1762+ void *_src, ptrdiff_t stride)
1763+{
1764+ TXSample *dst = _dst;
1765+ TXSample *src = _src;
1766+ const int len = s->len;
1767+ const int len2 = len >> 1;
1768+ const TXSample *exp = (void *)s->exp;
1769+ TXSample next;
1770+#ifdef TX_INT32
1771+ int64_t tmp1, tmp2;
1772+#else
1773+ TXSample tmp1, tmp2;
1774+#endif
1775+
1776+ for (int i = 0; i < len2; i++) {
1777+ TXSample in1 = src[i];
1778+ TXSample in2 = src[len - i - 1];
1779+ TXSample s = exp[len + i];
1780+
1781+#ifdef TX_INT32
1782+ tmp1 = in1 + in2;
1783+ tmp2 = in1 - in2;
1784+
1785+ tmp1 >>= 1;
1786+ tmp2 *= s;
1787+
1788+ tmp2 = (tmp2 + 0x40000000) >> 31;
1789+#else
1790+ tmp1 = (in1 + in2)*0.5;
1791+ tmp2 = (in1 - in2)*s;
1792+#endif
1793+
1794+ src[i] = tmp1 + tmp2;
1795+ src[len - i - 1] = tmp1 - tmp2;
1796+ }
1797+
1798+ s->fn[0](&s->sub[0], dst, src, sizeof(TXComplex));
1799+
1800+ next = dst[len];
1801+
1802+ for (int i = len - 2; i > 0; i -= 2) {
1803+ TXSample tmp;
1804+
1805+ CMUL(tmp, dst[i], exp[len - i], exp[i], dst[i + 0], dst[i + 1]);
1806+
1807+ dst[i + 1] = next;
1808+
1809+ next += tmp;
1810+ }
1811+
1812+#ifdef TX_INT32
1813+ tmp1 = ((int64_t)exp[0]) * ((int64_t)dst[0]);
1814+ dst[0] = (tmp1 + 0x40000000) >> 31;
1815+#else
1816+ dst[0] = exp[0] * dst[0];
1817+#endif
1818+ dst[1] = next;
1819+}
1820+
1821+static const FFTXCodelet TX_NAME(ff_tx_dctII_def) = {
1822+ .name = TX_NAME_STR("dctII"),
1823+ .function = TX_NAME(ff_tx_dctII),
1824+ .type = TX_TYPE(DCT),
1825+ .flags = AV_TX_UNALIGNED | AV_TX_INPLACE |
1826+ FF_TX_OUT_OF_PLACE | FF_TX_FORWARD_ONLY,
1827+ .factors = { 2, TX_FACTOR_ANY },
1828+ .min_len = 2,
1829+ .max_len = TX_LEN_UNLIMITED,
1830+ .init = TX_NAME(ff_tx_dct_init),
1831+ .cpu_flags = FF_TX_CPU_FLAGS_ALL,
1832+ .prio = FF_TX_PRIO_BASE,
1833+};
1834+
17281835 int TX_TAB(ff_tx_mdct_gen_exp)(AVTXContext *s, int *pre_tab)
17291836 {
17301837 int off = 0;
@@ -1812,6 +1919,7 @@ const FFTXCodelet * const TX_NAME(ff_tx_codelet_list)[] = { · 81 unmodified lines
18121919 &TX_NAME(ff_tx_mdct_inv_full_def),
18131920 &TX_NAME(ff_tx_rdft_r2c_def),
18141921 &TX_NAME(ff_tx_rdft_c2r_def),
1922+ &TX_NAME(ff_tx_dctII_def),
18151923
18161924 NULL,
18171925 };

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t