]> git.sesse.net Git - ffmpeg/blob - libavcodec/diracdsp.c
Merge commit '12b49769223234673db1003d9c43e7483ceb0282'
[ffmpeg] / libavcodec / diracdsp.c
1 /*
2  * Copyright (C) 2009 David Conrad
3  *
4  * This file is part of FFmpeg.
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include "avcodec.h"
22 #include "diracdsp.h"
23 #include "libavcodec/x86/diracdsp_mmx.h"
24
25 #define FILTER(src, stride)                                     \
26     ((21*((src)[ 0*stride] + (src)[1*stride])                   \
27       -7*((src)[-1*stride] + (src)[2*stride])                   \
28       +3*((src)[-2*stride] + (src)[3*stride])                   \
29       -1*((src)[-3*stride] + (src)[4*stride]) + 16) >> 5)
30
31 static void dirac_hpel_filter(uint8_t *dsth, uint8_t *dstv, uint8_t *dstc, const uint8_t *src,
32                               int stride, int width, int height)
33 {
34     int x, y;
35
36     for (y = 0; y < height; y++) {
37         for (x = -3; x < width+5; x++)
38             dstv[x] = av_clip_uint8(FILTER(src+x, stride));
39
40         for (x = 0; x < width; x++)
41             dstc[x] = av_clip_uint8(FILTER(dstv+x, 1));
42
43         for (x = 0; x < width; x++)
44             dsth[x] = av_clip_uint8(FILTER(src+x, 1));
45
46         src  += stride;
47         dsth += stride;
48         dstv += stride;
49         dstc += stride;
50     }
51 }
52
53 #define PIXOP_BILINEAR(PFX, OP, WIDTH)                                  \
54     static void ff_ ## PFX ## _dirac_pixels ## WIDTH ## _bilinear_c(uint8_t *dst, const uint8_t *src[5], int stride, int h) \
55     {                                                                   \
56         int x;                                                          \
57         const uint8_t *s0 = src[0];                                     \
58         const uint8_t *s1 = src[1];                                     \
59         const uint8_t *s2 = src[2];                                     \
60         const uint8_t *s3 = src[3];                                     \
61         const uint8_t *w  = src[4];                                     \
62                                                                         \
63         while (h--) {                                                   \
64             for (x = 0; x < WIDTH; x++) {                               \
65                 OP(dst[x], (s0[x]*w[0] + s1[x]*w[1] + s2[x]*w[2] + s3[x]*w[3] + 8) >> 4); \
66             }                                                           \
67                                                                         \
68             dst += stride;                                              \
69             s0 += stride;                                               \
70             s1 += stride;                                               \
71             s2 += stride;                                               \
72             s3 += stride;                                               \
73         }                                                               \
74     }
75
76 #define OP_PUT(dst, val) (dst) = (val)
77 #define OP_AVG(dst, val) (dst) = (((dst) + (val) + 1)>>1)
78
79 PIXOP_BILINEAR(put, OP_PUT, 8)
80 PIXOP_BILINEAR(put, OP_PUT, 16)
81 PIXOP_BILINEAR(put, OP_PUT, 32)
82 PIXOP_BILINEAR(avg, OP_AVG, 8)
83 PIXOP_BILINEAR(avg, OP_AVG, 16)
84 PIXOP_BILINEAR(avg, OP_AVG, 32)
85
86 #define op_scale1(x)  block[x] = av_clip_uint8( (block[x]*weight + (1<<(log2_denom-1))) >> log2_denom)
87 #define op_scale2(x)  dst[x] = av_clip_uint8( (src[x]*weights + dst[x]*weightd + (1<<(log2_denom-1))) >> log2_denom)
88
89 #define DIRAC_WEIGHT(W)                                                 \
90     static void weight_dirac_pixels ## W ## _c(uint8_t *block, int stride, int log2_denom, \
91                                                int weight, int h) {     \
92         int x;                                                          \
93         while (h--) {                                                   \
94             for (x = 0; x < W; x++) {                                   \
95                 op_scale1(x);                                           \
96                 op_scale1(x+1);                                         \
97             }                                                           \
98             block += stride;                                            \
99         }                                                               \
100     }                                                                   \
101     static void biweight_dirac_pixels ## W ## _c(uint8_t *dst, const uint8_t *src, int stride, int log2_denom, \
102                                                  int weightd, int weights, int h) { \
103         int x;                                                          \
104         while (h--) {                                                   \
105             for (x = 0; x < W; x++) {                                   \
106                 op_scale2(x);                                           \
107                 op_scale2(x+1);                                         \
108             }                                                           \
109             dst += stride;                                              \
110             src += stride;                                              \
111         }                                                               \
112     }
113
114 DIRAC_WEIGHT(8)
115 DIRAC_WEIGHT(16)
116 DIRAC_WEIGHT(32)
117
118 #define ADD_OBMC(xblen)                                                 \
119     static void add_obmc ## xblen ## _c(uint16_t *dst, const uint8_t *src, int stride, \
120                                         const uint8_t *obmc_weight, int yblen) \
121     {                                                                   \
122         int x;                                                          \
123         while (yblen--) {                                               \
124             for (x = 0; x < xblen; x += 2) {                            \
125                 dst[x  ] += src[x  ] * obmc_weight[x  ];                \
126                 dst[x+1] += src[x+1] * obmc_weight[x+1];                \
127             }                                                           \
128             dst += stride;                                              \
129             src += stride;                                              \
130             obmc_weight += 32;                                          \
131         }                                                               \
132     }
133
134 ADD_OBMC(8)
135 ADD_OBMC(16)
136 ADD_OBMC(32)
137
138 static void put_signed_rect_clamped_8bit_c(uint8_t *dst, int dst_stride, const uint8_t *_src, int src_stride, int width, int height)
139 {
140     int x, y;
141     int16_t *src = (int16_t *)_src;
142     for (y = 0; y < height; y++) {
143         for (x = 0; x < width; x+=4) {
144             dst[x  ] = av_clip_uint8(src[x  ] + 128);
145             dst[x+1] = av_clip_uint8(src[x+1] + 128);
146             dst[x+2] = av_clip_uint8(src[x+2] + 128);
147             dst[x+3] = av_clip_uint8(src[x+3] + 128);
148         }
149         dst += dst_stride;
150         src += src_stride >> 1;
151     }
152 }
153
154 #define PUT_SIGNED_RECT_CLAMPED(PX)                                                                     \
155 static void put_signed_rect_clamped_ ## PX ## bit_c(uint8_t *_dst, int dst_stride, const uint8_t *_src, \
156                                                   int src_stride, int width, int height)                \
157 {                                                                                                       \
158     int x, y;                                                                                           \
159     uint16_t *dst = (uint16_t *)_dst;                                                                   \
160     int32_t *src = (int32_t *)_src;                                                                     \
161     for (y = 0; y < height; y++) {                                                                      \
162         for (x = 0; x < width; x+=4) {                                                                  \
163             dst[x  ] = av_clip_uintp2(src[x  ] + (1 << (PX - 1)), PX);                                  \
164             dst[x+1] = av_clip_uintp2(src[x+1] + (1 << (PX - 1)), PX);                                  \
165             dst[x+2] = av_clip_uintp2(src[x+2] + (1 << (PX - 1)), PX);                                  \
166             dst[x+3] = av_clip_uintp2(src[x+3] + (1 << (PX - 1)), PX);                                  \
167         }                                                                                               \
168         dst += dst_stride >> 1;                                                                         \
169         src += src_stride >> 2;                                                                         \
170     }                                                                                                   \
171 }
172
173 PUT_SIGNED_RECT_CLAMPED(10)
174 PUT_SIGNED_RECT_CLAMPED(12)
175
176 static void add_rect_clamped_c(uint8_t *dst, const uint16_t *src, int stride,
177                                const int16_t *idwt, int idwt_stride,
178                                int width, int height)
179 {
180     int x, y;
181
182     for (y = 0; y < height; y++) {
183         for (x = 0; x < width; x+=2) {
184             dst[x  ] = av_clip_uint8(((src[x  ]+32)>>6) + idwt[x  ]);
185             dst[x+1] = av_clip_uint8(((src[x+1]+32)>>6) + idwt[x+1]);
186         }
187         dst += stride;
188         src += stride;
189         idwt += idwt_stride;
190     }
191 }
192
193 #define PIXFUNC(PFX, WIDTH)                                             \
194     c->PFX ## _dirac_pixels_tab[WIDTH>>4][0] = ff_ ## PFX ## _dirac_pixels ## WIDTH ## _c; \
195     c->PFX ## _dirac_pixels_tab[WIDTH>>4][1] = ff_ ## PFX ## _dirac_pixels ## WIDTH ## _l2_c; \
196     c->PFX ## _dirac_pixels_tab[WIDTH>>4][2] = ff_ ## PFX ## _dirac_pixels ## WIDTH ## _l4_c; \
197     c->PFX ## _dirac_pixels_tab[WIDTH>>4][3] = ff_ ## PFX ## _dirac_pixels ## WIDTH ## _bilinear_c
198
199 av_cold void ff_diracdsp_init(DiracDSPContext *c)
200 {
201     c->dirac_hpel_filter = dirac_hpel_filter;
202     c->add_rect_clamped = add_rect_clamped_c;
203     c->put_signed_rect_clamped[0] = put_signed_rect_clamped_8bit_c;
204     c->put_signed_rect_clamped[1] = put_signed_rect_clamped_10bit_c;
205     c->put_signed_rect_clamped[2] = put_signed_rect_clamped_12bit_c;
206
207     c->add_dirac_obmc[0] = add_obmc8_c;
208     c->add_dirac_obmc[1] = add_obmc16_c;
209     c->add_dirac_obmc[2] = add_obmc32_c;
210
211     c->weight_dirac_pixels_tab[0] = weight_dirac_pixels8_c;
212     c->weight_dirac_pixels_tab[1] = weight_dirac_pixels16_c;
213     c->weight_dirac_pixels_tab[2] = weight_dirac_pixels32_c;
214     c->biweight_dirac_pixels_tab[0] = biweight_dirac_pixels8_c;
215     c->biweight_dirac_pixels_tab[1] = biweight_dirac_pixels16_c;
216     c->biweight_dirac_pixels_tab[2] = biweight_dirac_pixels32_c;
217
218     PIXFUNC(put, 8);
219     PIXFUNC(put, 16);
220     PIXFUNC(put, 32);
221     PIXFUNC(avg, 8);
222     PIXFUNC(avg, 16);
223     PIXFUNC(avg, 32);
224
225     if (HAVE_MMX && HAVE_YASM) ff_diracdsp_init_mmx(c);
226 }