1 /*
2  * Common stuff for some Microsoft Screen codecs
3  * Copyright (C) 2012 Konstantin Shishkov
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 #include <stdint.h>
23 #include "libavutil/common.h"
24 #include "mss34dsp.h"
25 
26 static const uint8_t luma_quant[64] = {
27     16,  11,  10,  16,  24,  40,  51,  61,
28     12,  12,  14,  19,  26,  58,  60,  55,
29     14,  13,  16,  24,  40,  57,  69,  56,
30     14,  17,  22,  29,  51,  87,  80,  62,
31     18,  22,  37,  56,  68, 109, 103,  77,
32     24,  35,  55,  64,  81, 104, 113,  92,
33     49,  64,  78,  87, 103, 121, 120, 101,
34     72,  92,  95,  98, 112, 100, 103,  99
35 };
36 
37 static const uint8_t chroma_quant[64] = {
38     17, 18, 24, 47, 99, 99, 99, 99,
39     18, 21, 26, 66, 99, 99, 99, 99,
40     24, 26, 56, 99, 99, 99, 99, 99,
41     47, 66, 99, 99, 99, 99, 99, 99,
42     99, 99, 99, 99, 99, 99, 99, 99,
43     99, 99, 99, 99, 99, 99, 99, 99,
44     99, 99, 99, 99, 99, 99, 99, 99,
45     99, 99, 99, 99, 99, 99, 99, 99
46 };
47 
ff_mss34_gen_quant_mat(uint16_t * qmat,int quality,int luma)48 void ff_mss34_gen_quant_mat(uint16_t *qmat, int quality, int luma)
49 {
50     int i;
51     const uint8_t *qsrc = luma ? luma_quant : chroma_quant;
52 
53     if (quality >= 50) {
54         int scale = 200 - 2 * quality;
55 
56         for (i = 0; i < 64; i++)
57             qmat[i] = (qsrc[i] * scale + 50) / 100;
58     } else {
59         for (i = 0; i < 64; i++)
60             qmat[i] = (5000 * qsrc[i] / quality + 50) / 100;
61     }
62 }
63 
64 #define DCT_TEMPLATE(blk, step, SOP, shift)                         \
65     const unsigned t0 =-39409U * blk[7 * step] - 58980U * blk[1 * step]; \
66     const unsigned t1 = 39410U * blk[1 * step] - 58980U * blk[7 * step]; \
67     const unsigned t2 =-33410U * blk[5 * step] -167963U * blk[3 * step]; \
68     const unsigned t3 = 33410U * blk[3 * step] -167963U * blk[5 * step]; \
69     const unsigned t4 =          blk[3 * step] +          blk[7 * step]; \
70     const unsigned t5 =          blk[1 * step] +          blk[5 * step]; \
71     const unsigned t6 = 77062U * t4            + 51491U * t5;            \
72     const unsigned t7 = 77062U * t5            - 51491U * t4;            \
73     const unsigned t8 = 35470U * blk[2 * step] - 85623U * blk[6 * step]; \
74     const unsigned t9 = 35470U * blk[6 * step] + 85623U * blk[2 * step]; \
75     const unsigned tA = SOP(blk[0 * step] - blk[4 * step]);              \
76     const unsigned tB = SOP(blk[0 * step] + blk[4 * step]);              \
77                                                                     \
78     blk[0 * step] = (int)(  t1 + t6  + t9 + tB) >> shift;                \
79     blk[1 * step] = (int)(  t3 + t7  + t8 + tA) >> shift;                \
80     blk[2 * step] = (int)(  t2 + t6  - t8 + tA) >> shift;                \
81     blk[3 * step] = (int)(  t0 + t7  - t9 + tB) >> shift;                \
82     blk[4 * step] = (int)(-(t0 + t7) - t9 + tB) >> shift;                \
83     blk[5 * step] = (int)(-(t2 + t6) - t8 + tA) >> shift;                \
84     blk[6 * step] = (int)(-(t3 + t7) + t8 + tA) >> shift;                \
85     blk[7 * step] = (int)(-(t1 + t6) + t9 + tB) >> shift;                \
86 
87 #define SOP_ROW(a) (((a) * (1U << 16)) + 0x2000)
88 #define SOP_COL(a) (((a) + 32) * (1U << 16))
89 
ff_mss34_dct_put(uint8_t * dst,ptrdiff_t stride,int * block)90 void ff_mss34_dct_put(uint8_t *dst, ptrdiff_t stride, int *block)
91 {
92     int i, j;
93     int *ptr;
94 
95     ptr = block;
96     for (i = 0; i < 8; i++) {
97         DCT_TEMPLATE(ptr, 1, SOP_ROW, 13);
98         ptr += 8;
99     }
100 
101     ptr = block;
102     for (i = 0; i < 8; i++) {
103         DCT_TEMPLATE(ptr, 8, SOP_COL, 22);
104         ptr++;
105     }
106 
107     ptr = block;
108     for (j = 0; j < 8; j++) {
109         for (i = 0; i < 8; i++)
110             dst[i] = av_clip_uint8(ptr[i] + 128);
111         dst += stride;
112         ptr += 8;
113     }
114 }
115