summaryrefslogtreecommitdiff
path: root/libavcodec/cfhddsp.c
blob: a141db5246999c35e52d251f54d3125483bbb710 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
 * Copyright (c) 2015-2016 Kieran Kunhya <kieran@kunhya.com>
 *
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * FFmpeg is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with FFmpeg; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#include "libavutil/attributes.h"
#include "libavutil/common.h"

#include "cfhddsp.h"

static av_always_inline void filter(int16_t *output, ptrdiff_t out_stride,
                          const int16_t *low, ptrdiff_t low_stride,
                          const int16_t *high, ptrdiff_t high_stride,
                          int len, int clip)
{
    int16_t tmp;
    int i;

    tmp = (11*low[0*low_stride] - 4*low[1*low_stride] + low[2*low_stride] + 4) >> 3;
    output[(2*0+0)*out_stride] = (tmp + high[0*high_stride]) >> 1;
    if (clip)
        output[(2*0+0)*out_stride] = av_clip_uintp2_c(output[(2*0+0)*out_stride], clip);

    tmp = ( 5*low[0*low_stride] + 4*low[1*low_stride] - low[2*low_stride] + 4) >> 3;
    output[(2*0+1)*out_stride] = (tmp - high[0*high_stride]) >> 1;
    if (clip)
        output[(2*0+1)*out_stride] = av_clip_uintp2_c(output[(2*0+1)*out_stride], clip);

    for (i = 1; i < len - 1; i++) {
        tmp = (low[(i-1)*low_stride] - low[(i+1)*low_stride] + 4) >> 3;
        output[(2*i+0)*out_stride] = (tmp + low[i*low_stride] + high[i*high_stride]) >> 1;
        if (clip)
            output[(2*i+0)*out_stride] = av_clip_uintp2_c(output[(2*i+0)*out_stride], clip);

        tmp = (low[(i+1)*low_stride] - low[(i-1)*low_stride] + 4) >> 3;
        output[(2*i+1)*out_stride] = (tmp + low[i*low_stride] - high[i*high_stride]) >> 1;
        if (clip)
            output[(2*i+1)*out_stride] = av_clip_uintp2_c(output[(2*i+1)*out_stride], clip);
    }

    tmp = ( 5*low[i*low_stride] + 4*low[(i-1)*low_stride] - low[(i-2)*low_stride] + 4) >> 3;
    output[(2*i+0)*out_stride] = (tmp + high[i*high_stride]) >> 1;
    if (clip)
        output[(2*i+0)*out_stride] = av_clip_uintp2_c(output[(2*i+0)*out_stride], clip);

    tmp = (11*low[i*low_stride] - 4*low[(i-1)*low_stride] + low[(i-2)*low_stride] + 4) >> 3;
    output[(2*i+1)*out_stride] = (tmp - high[i*high_stride]) >> 1;
    if (clip)
        output[(2*i+1)*out_stride] = av_clip_uintp2_c(output[(2*i+1)*out_stride], clip);
}

static void vert_filter(int16_t *output, ptrdiff_t out_stride,
                        const int16_t *low, ptrdiff_t low_stride,
                        const int16_t *high, ptrdiff_t high_stride,
                        int width, int height)
{
    for (int i = 0; i < width; i++) {
        filter(output, out_stride, low, low_stride, high, high_stride, height, 0);
        low++;
        high++;
        output++;
    }
}

static void horiz_filter(int16_t *output, ptrdiff_t ostride,
                         const int16_t *low, ptrdiff_t lstride,
                         const int16_t *high, ptrdiff_t hstride,
                         int width, int height)
{
    for (int i = 0; i < height; i++) {
        filter(output, 1, low, 1, high, 1, width, 0);
        low    += lstride;
        high   += hstride;
        output += ostride * 2;
    }
}

static void horiz_filter_clip(int16_t *output, const int16_t *low, const int16_t *high,
                              int width, int clip)
{
    filter(output, 1, low, 1, high, 1, width, clip);
}

static void horiz_filter_clip_bayer(int16_t *output, const int16_t *low, const int16_t *high,
                                    int width, int clip)
{
    filter(output, 2, low, 1, high, 1, width, clip);
}

av_cold void ff_cfhddsp_init(CFHDDSPContext *c, int depth, int bayer)
{
    c->horiz_filter = horiz_filter;
    c->vert_filter = vert_filter;

    if (bayer)
        c->horiz_filter_clip = horiz_filter_clip_bayer;
    else
        c->horiz_filter_clip = horiz_filter_clip;

#if ARCH_X86
    ff_cfhddsp_init_x86(c, depth, bayer);
#endif
}