hevcpred_template.c 22.3 KB
Newer Older
G
Guillaume Martres 已提交
1
/*
2
 * HEVC video decoder
G
Guillaume Martres 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 *
 * Copyright (C) 2012 - 2013 Guillaume Martres
 *
 * 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/pixdesc.h"
24

G
Guillaume Martres 已提交
25 26 27 28 29 30 31 32 33 34
#include "bit_depth_template.c"
#include "hevcpred.h"

#define POS(x, y) src[(x) + stride * (y)]

static void FUNC(intra_pred)(HEVCContext *s, int x0, int y0, int log2_size, int c_idx)
{
#define PU(x) \
    ((x) >> s->sps->log2_min_pu_size)
#define MVF(x, y) \
35
    (s->ref->tab_mvf[(x) + (y) * min_pu_width])
G
Guillaume Martres 已提交
36 37 38
#define MVF_PU(x, y) \
    MVF(PU(x0 + ((x) << hshift)), PU(y0 + ((y) << vshift)))
#define IS_INTRA(x, y) \
39
    (MVF_PU(x, y).pred_flag == PF_INTRA)
G
Guillaume Martres 已提交
40 41
#define MIN_TB_ADDR_ZS(x, y) \
    s->pps->min_tb_addr_zs[(y) * s->sps->min_tb_width + (x)]
42
#define EXTEND(ptr, start, length)                                             \
43
        for (i = start; i < (start) + (length); i += 4)                        \
44 45
            AV_WN4P(&(ptr[i]), a)
#define EXTEND_RIGHT_CIP(ptr, start, length)                                   \
46
        for (i = start; i < (start) + (length); i += 4)                        \
47
            if (!IS_INTRA(i, -1))                                              \
48
                AV_WN4P(&ptr[i], a);                                           \
49 50
            else                                                               \
                a = PIXEL_SPLAT_X4(ptr[i+3])
G
Guillaume Martres 已提交
51
#define EXTEND_LEFT_CIP(ptr, start, length) \
52
        for (i = start; i > (start) - (length); i--) \
G
Guillaume Martres 已提交
53 54
            if (!IS_INTRA(i - 1, -1)) \
                ptr[i - 1] = ptr[i]
55
#define EXTEND_UP_CIP(ptr, start, length)                                      \
56
        for (i = (start); i > (start) - (length); i -= 4)                      \
57
            if (!IS_INTRA(-1, i - 3))                                          \
58
                AV_WN4P(&ptr[i - 3], a);                                       \
59 60
            else                                                               \
                a = PIXEL_SPLAT_X4(ptr[i - 3])
61 62
#define EXTEND_DOWN_CIP(ptr, start, length)                                    \
        for (i = start; i < (start) + (length); i += 4)                        \
63
            if (!IS_INTRA(-1, i))                                              \
64
                AV_WN4P(&ptr[i], a);                                           \
65 66 67
            else                                                               \
                a = PIXEL_SPLAT_X4(ptr[i + 3])

68
    HEVCLocalContext *lc = s->HEVClc;
G
Guillaume Martres 已提交
69 70 71 72
    int i;
    int hshift = s->sps->hshift[c_idx];
    int vshift = s->sps->vshift[c_idx];
    int size = (1 << log2_size);
73
    int size_in_luma_h = size << hshift;
74
    int size_in_tbs_h  = size_in_luma_h >> s->sps->log2_min_tb_size;
75
    int size_in_luma_v = size << vshift;
76
    int size_in_tbs_v  = size_in_luma_v >> s->sps->log2_min_tb_size;
G
Guillaume Martres 已提交
77 78
    int x = x0 >> hshift;
    int y = y0 >> vshift;
79 80
    int x_tb = x0 >> s->sps->log2_min_tb_size;
    int y_tb = y0 >> s->sps->log2_min_tb_size;
G
Guillaume Martres 已提交
81 82 83 84 85
    int cur_tb_addr = MIN_TB_ADDR_ZS(x_tb, y_tb);

    ptrdiff_t stride = s->frame->linesize[c_idx] / sizeof(pixel);
    pixel *src = (pixel*)s->frame->data[c_idx] + x + y * stride;

86
    int min_pu_width = s->sps->min_pu_width;
G
Guillaume Martres 已提交
87 88 89

    enum IntraPredMode mode = c_idx ? lc->pu.intra_pred_mode_c :
                              lc->tu.cur_intra_pred_mode;
90
    pixel4 a;
91 92 93 94
    pixel  left_array[2 * MAX_TB_SIZE + 1];
    pixel  filtered_left_array[2 * MAX_TB_SIZE + 1];
    pixel  top_array[2 * MAX_TB_SIZE + 1];
    pixel  filtered_top_array[2 * MAX_TB_SIZE + 1];
G
Guillaume Martres 已提交
95

96 97 98 99
    pixel  *left          = left_array + 1;
    pixel  *top           = top_array  + 1;
    pixel  *filtered_left = filtered_left_array + 1;
    pixel  *filtered_top  = filtered_top_array  + 1;
G
Guillaume Martres 已提交
100

101
    int cand_bottom_left = lc->na.cand_bottom_left && cur_tb_addr > MIN_TB_ADDR_ZS(x_tb - 1, y_tb + size_in_tbs_v);
G
Guillaume Martres 已提交
102 103 104
    int cand_left        = lc->na.cand_left;
    int cand_up_left     = lc->na.cand_up_left;
    int cand_up          = lc->na.cand_up;
105
    int cand_up_right    = lc->na.cand_up_right && cur_tb_addr > MIN_TB_ADDR_ZS(x_tb + size_in_tbs_h, y_tb - 1);
G
Guillaume Martres 已提交
106

107
    int bottom_left_size = (FFMIN(y0 + 2 * size_in_luma_v, s->sps->height) -
108
                           (y0 + size_in_luma_v)) >> vshift;
109
    int top_right_size   = (FFMIN(x0 + 2 * size_in_luma_h, s->sps->width) -
110
                           (x0 + size_in_luma_h)) >> hshift;
G
Guillaume Martres 已提交
111 112

    if (s->pps->constrained_intra_pred_flag == 1) {
113 114
        int size_in_luma_pu_v = PU(size_in_luma_v);
        int size_in_luma_pu_h = PU(size_in_luma_h);
115 116
        int on_pu_edge_x    = !(x0 & ((1 << s->sps->log2_min_pu_size) - 1));
        int on_pu_edge_y    = !(y0 & ((1 << s->sps->log2_min_pu_size) - 1));
117 118
        if (!size_in_luma_pu_h)
            size_in_luma_pu_h++;
G
Guillaume Martres 已提交
119 120
        if (cand_bottom_left == 1 && on_pu_edge_x) {
            int x_left_pu   = PU(x0 - 1);
121 122
            int y_bottom_pu = PU(y0 + size_in_luma_v);
            int max = FFMIN(size_in_luma_pu_v, s->sps->min_pu_height - y_bottom_pu);
G
Guillaume Martres 已提交
123
            cand_bottom_left = 0;
124
            for (i = 0; i < max; i += 2)
125
                cand_bottom_left |= (MVF(x_left_pu, y_bottom_pu + i).pred_flag == PF_INTRA);
G
Guillaume Martres 已提交
126 127 128 129
        }
        if (cand_left == 1 && on_pu_edge_x) {
            int x_left_pu   = PU(x0 - 1);
            int y_left_pu   = PU(y0);
130
            int max = FFMIN(size_in_luma_pu_v, s->sps->min_pu_height - y_left_pu);
G
Guillaume Martres 已提交
131
            cand_left = 0;
132
            for (i = 0; i < max; i += 2)
133
                cand_left |= (MVF(x_left_pu, y_left_pu + i).pred_flag == PF_INTRA);
G
Guillaume Martres 已提交
134 135 136 137
        }
        if (cand_up_left == 1) {
            int x_left_pu   = PU(x0 - 1);
            int y_top_pu    = PU(y0 - 1);
138
            cand_up_left = MVF(x_left_pu, y_top_pu).pred_flag == PF_INTRA;
G
Guillaume Martres 已提交
139 140 141 142
        }
        if (cand_up == 1 && on_pu_edge_y) {
            int x_top_pu    = PU(x0);
            int y_top_pu    = PU(y0 - 1);
143
            int max = FFMIN(size_in_luma_pu_h, s->sps->min_pu_width - x_top_pu);
G
Guillaume Martres 已提交
144
            cand_up = 0;
145
            for (i = 0; i < max; i += 2)
146
                cand_up |= (MVF(x_top_pu + i, y_top_pu).pred_flag == PF_INTRA);
G
Guillaume Martres 已提交
147 148 149
        }
        if (cand_up_right == 1 && on_pu_edge_y) {
            int y_top_pu    = PU(y0 - 1);
150 151
            int x_right_pu  = PU(x0 + size_in_luma_h);
            int max = FFMIN(size_in_luma_pu_h, s->sps->min_pu_width - x_right_pu);
G
Guillaume Martres 已提交
152
            cand_up_right = 0;
153
            for (i = 0; i < max; i += 2)
154
                cand_up_right |= (MVF(x_right_pu + i, y_top_pu).pred_flag == PF_INTRA);
G
Guillaume Martres 已提交
155
        }
156 157
        memset(left, 128, 2 * MAX_TB_SIZE*sizeof(pixel));
        memset(top , 128, 2 * MAX_TB_SIZE*sizeof(pixel));
158
        top[-1] = 128;
G
Guillaume Martres 已提交
159
    }
160 161 162
    if (cand_up_left) {
        left[-1] = POS(-1, -1);
        top[-1]  = left[-1];
G
Guillaume Martres 已提交
163 164
    }
    if (cand_up)
165 166 167
        for (i = 0; i <size; i+=4)
            AV_WN4P(&top[i], AV_RN4P(&POS(i, -1)));

G
Guillaume Martres 已提交
168
    if (cand_up_right) {
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
        a = PIXEL_SPLAT_X4(POS(size + top_right_size - 1, -1));
        for (i = size + top_right_size; i < (size << 1); i += 4)
            AV_WN4P(&top[i], a);
        for (i = size ; i < size+top_right_size; i+=4)
            AV_WN4P(&top[i], AV_RN4P(&POS(i, -1)));
    }
    if (cand_left)
        for (i = 0; i < size; i++)
            left[i] = POS(-1, i);
    if (cand_bottom_left) {
        for (i = size ; i < size+bottom_left_size; i++)
            left[i] = POS(-1, i);
        a = PIXEL_SPLAT_X4(POS(-1, size + bottom_left_size - 1));
        for (i = size + bottom_left_size; i < (size << 1); i+=4)
            AV_WN4P(&left[i], a);
G
Guillaume Martres 已提交
184 185 186 187 188 189 190 191 192
    }

    if (s->pps->constrained_intra_pred_flag == 1) {
        if (cand_bottom_left || cand_left || cand_up_left || cand_up || cand_up_right) {
            int size_max_x = x0 + ((2 * size) << hshift) < s->sps->width ?
                                    2 * size : (s->sps->width - x0) >> hshift;
            int size_max_y = y0 + ((2 * size) << vshift) < s->sps->height ?
                                    2 * size : (s->sps->height - y0) >> vshift;
            int j = size + (cand_bottom_left? bottom_left_size: 0) -1;
193 194 195 196 197 198 199 200
            if (!cand_up_right) {
                size_max_x = x0 + ((size) << hshift) < s->sps->width ?
                                                    size : (s->sps->width - x0) >> hshift;
            }
            if (!cand_bottom_left) {
                size_max_y = y0 + (( size) << vshift) < s->sps->height ?
                                                     size : (s->sps->height - y0) >> vshift;
            }
G
Guillaume Martres 已提交
201
            if (cand_bottom_left || cand_left || cand_up_left) {
202 203
                while (j > -1 && !IS_INTRA(-1, j))
                    j--;
G
Guillaume Martres 已提交
204 205
                if (!IS_INTRA(-1, j)) {
                    j = 0;
206 207 208
                    while (j < size_max_x && !IS_INTRA(j, -1))
                        j++;
                    EXTEND_LEFT_CIP(top, j, j + 1);
G
Guillaume Martres 已提交
209
                    left[-1] = top[-1];
210
                    j        = 0;
G
Guillaume Martres 已提交
211 212 213
                }
            } else {
                j = 0;
214 215
                while (j < size_max_x && !IS_INTRA(j, -1))
                    j++;
G
Guillaume Martres 已提交
216
                if (j > 0)
217
                    if (x0 > 0) {
218
                        EXTEND_LEFT_CIP(top, j, j + 1);
219 220 221 222
                    } else {
                        EXTEND_LEFT_CIP(top, j, j);
                        top[-1] = top[0];
                    }
G
Guillaume Martres 已提交
223
                left[-1] = top[-1];
224
                j        = 0;
G
Guillaume Martres 已提交
225
            }
226
            left[-1] = top[-1];
G
Guillaume Martres 已提交
227
            if (cand_bottom_left || cand_left) {
228 229
                a = PIXEL_SPLAT_X4(left[-1]);
                EXTEND_DOWN_CIP(left, 0, size_max_y);
G
Guillaume Martres 已提交
230 231
            }
            if (!cand_left) {
232 233
                a = PIXEL_SPLAT_X4(left[-1]);
                EXTEND(left, 0, size);
G
Guillaume Martres 已提交
234 235
            }
            if (!cand_bottom_left) {
236 237
                a = PIXEL_SPLAT_X4(left[size - 1]);
                EXTEND(left, size, size);
G
Guillaume Martres 已提交
238 239
            }
            if (x0 != 0 && y0 != 0) {
240
                a = PIXEL_SPLAT_X4(left[size_max_y - 1]);
G
Guillaume Martres 已提交
241
                EXTEND_UP_CIP(left, size_max_y - 1, size_max_y);
242 243
                if (!IS_INTRA(-1, - 1))
                    left[-1] = left[0];
244
            } else if (x0 == 0) {
245 246
                a = PIXEL_SPLAT_X4(left[size_max_y - 1]);
                EXTEND(left, 0, size_max_y);
247
            } else {
248 249
                a = PIXEL_SPLAT_X4(left[size_max_y - 1]);
                EXTEND_UP_CIP(left, size_max_y - 1, size_max_y);
G
Guillaume Martres 已提交
250 251 252
            }
            top[-1] = left[-1];
            if (y0 != 0) {
253
                a = PIXEL_SPLAT_X4(left[-1]);
G
Guillaume Martres 已提交
254 255 256 257 258 259 260
                EXTEND_RIGHT_CIP(top, 0, size_max_x);
            }
        }
    }
    // Infer the unavailable samples
    if (!cand_bottom_left) {
        if (cand_left) {
261 262
            a = PIXEL_SPLAT_X4(left[size-1]);
            EXTEND(left, size, size);
G
Guillaume Martres 已提交
263
        } else if (cand_up_left) {
264 265
            a = PIXEL_SPLAT_X4(left[-1]);
            EXTEND(left, 0, 2 * size);
G
Guillaume Martres 已提交
266 267 268
            cand_left = 1;
        } else if (cand_up) {
            left[-1] = top[0];
269 270
            a = PIXEL_SPLAT_X4(left[-1]);
            EXTEND(left, 0, 2 * size);
G
Guillaume Martres 已提交
271
            cand_up_left = 1;
272
            cand_left    = 1;
G
Guillaume Martres 已提交
273
        } else if (cand_up_right) {
274 275 276 277
            left[-1] = top[size];
            a = PIXEL_SPLAT_X4(left[-1]);
            EXTEND(top, 0, size);
            EXTEND(left, 0, 2 * size);
278
            cand_up      = 1;
G
Guillaume Martres 已提交
279
            cand_up_left = 1;
280
            cand_left    = 1;
G
Guillaume Martres 已提交
281
        } else { // No samples available
282 283 284 285
            left[-1] = (1 << (BIT_DEPTH - 1));
            a = PIXEL_SPLAT_X4(left[-1]);
            EXTEND(top, 0, 2 * size);
            EXTEND(left, 0, 2 * size);
G
Guillaume Martres 已提交
286 287 288 289
        }
    }

    if (!cand_left) {
290 291
        a = PIXEL_SPLAT_X4(left[size]);
        EXTEND(left, 0, size);
G
Guillaume Martres 已提交
292 293 294 295 296
    }
    if (!cand_up_left) {
        left[-1] = left[0];
    }
    if (!cand_up) {
297 298
        a = PIXEL_SPLAT_X4(left[-1]);
        EXTEND(top, 0, size);
G
Guillaume Martres 已提交
299 300
    }
    if (!cand_up_right) {
301 302
        a = PIXEL_SPLAT_X4(top[size-1]);
        EXTEND(top, size, size);
G
Guillaume Martres 已提交
303 304 305 306 307
    }

    top[-1] = left[-1];

    // Filtering process
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
    if (c_idx == 0) {
        if (mode != INTRA_DC && size != 4){
            int intra_hor_ver_dist_thresh[] = { 7, 1, 0 };
            int min_dist_vert_hor = FFMIN(FFABS((int)(mode - 26U)),
                                          FFABS((int)(mode - 10U)));
            if (min_dist_vert_hor > intra_hor_ver_dist_thresh[log2_size - 3]) {
                int threshold = 1 << (BIT_DEPTH - 5);
                if (s->sps->sps_strong_intra_smoothing_enable_flag && c_idx == 0 &&
                    log2_size == 5 &&
                    FFABS(top[-1]  + top[63]  - 2 * top[31])  < threshold &&
                    FFABS(left[-1] + left[63] - 2 * left[31]) < threshold) {
                    // We can't just overwrite values in top because it could be
                    // a pointer into src
                    filtered_top[-1] = top[-1];
                    filtered_top[63] = top[63];
                    for (i = 0; i < 63; i++)
                        filtered_top[i] = ((64 - (i + 1)) * top[-1] +
                                           (i + 1)  * top[63] + 32) >> 6;
                    for (i = 0; i < 63; i++)
                        left[i] = ((64 - (i + 1)) * left[-1] +
                                   (i + 1)  * left[63] + 32) >> 6;
                    top = filtered_top;
                } else {
                    filtered_left[2 * size - 1] = left[2 * size - 1];
                    filtered_top[2 * size - 1]  = top[2 * size - 1];
                    for (i = 2 * size - 2; i >= 0; i--)
                        filtered_left[i] = (left[i + 1] + 2 * left[i] +
                                            left[i - 1] + 2) >> 2;
                    filtered_top[-1]  =
                    filtered_left[-1] = (left[0] + 2 * left[-1] + top[0] + 2) >> 2;
                    for (i = 2 * size - 2; i >= 0; i--)
                        filtered_top[i] = (top[i + 1] + 2 * top[i] +
                                           top[i - 1] + 2) >> 2;
                    left = filtered_left;
                    top  = filtered_top;
                }
G
Guillaume Martres 已提交
344 345 346 347 348 349
            }
        }
    }

    switch (mode) {
    case INTRA_PLANAR:
350 351
        s->hpc.pred_planar[log2_size - 2]((uint8_t *)src, (uint8_t *)top,
                                          (uint8_t *)left, stride);
G
Guillaume Martres 已提交
352 353
        break;
    case INTRA_DC:
354 355
        s->hpc.pred_dc((uint8_t *)src, (uint8_t *)top,
                       (uint8_t *)left, stride, log2_size, c_idx);
G
Guillaume Martres 已提交
356 357
        break;
    default:
358 359 360
        s->hpc.pred_angular[log2_size - 2]((uint8_t *)src, (uint8_t *)top,
                                           (uint8_t *)left, stride, c_idx,
                                           mode);
G
Guillaume Martres 已提交
361 362 363 364
        break;
    }
}

365 366
static av_always_inline void FUNC(pred_planar)(uint8_t *_src, const uint8_t *_top,
                              const uint8_t *_left, ptrdiff_t stride, int trafo_size)
G
Guillaume Martres 已提交
367 368
{
    int x, y;
369 370 371
    pixel *src        = (pixel *)_src;
    const pixel *top  = (const pixel *)_top;
    const pixel *left = (const pixel *)_left;
372 373 374 375 376 377 378 379 380 381 382
    int size = 1 << trafo_size;
    for (y = 0; y < size; y++)
        for (x = 0; x < size; x++)
            POS(x, y) = ((size - 1 - x) * left[y] + (x + 1) * top[size]  +
                         (size - 1 - y) * top[x]  + (y + 1) * left[size] + size) >> (trafo_size + 1);
}

static void FUNC(pred_planar_0)(uint8_t *_src, const uint8_t *_top,
                                const uint8_t *_left, ptrdiff_t stride)
{
    FUNC(pred_planar)(_src, _top, _left, stride, 2);
G
Guillaume Martres 已提交
383 384 385 386 387
}

static void FUNC(pred_planar_1)(uint8_t *_src, const uint8_t *_top,
                                const uint8_t *_left, ptrdiff_t stride)
{
388
    FUNC(pred_planar)(_src, _top, _left, stride, 3);
G
Guillaume Martres 已提交
389 390 391 392 393
}

static void FUNC(pred_planar_2)(uint8_t *_src, const uint8_t *_top,
                                const uint8_t *_left, ptrdiff_t stride)
{
394
    FUNC(pred_planar)(_src, _top, _left, stride, 4);
G
Guillaume Martres 已提交
395 396 397 398 399
}

static void FUNC(pred_planar_3)(uint8_t *_src, const uint8_t *_top,
                                const uint8_t *_left, ptrdiff_t stride)
{
400
    FUNC(pred_planar)(_src, _top, _left, stride, 5);
G
Guillaume Martres 已提交
401 402 403 404 405 406 407
}

static void FUNC(pred_dc)(uint8_t *_src, const uint8_t *_top,
                          const uint8_t *_left,
                          ptrdiff_t stride, int log2_size, int c_idx)
{
    int i, j, x, y;
408 409 410 411 412
    int size          = (1 << log2_size);
    pixel *src        = (pixel *)_src;
    const pixel *top  = (const pixel *)_top;
    const pixel *left = (const pixel *)_left;
    int dc            = size;
G
Guillaume Martres 已提交
413 414 415 416 417 418 419 420 421
    pixel4 a;
    for (i = 0; i < size; i++)
        dc += left[i] + top[i];

    dc >>= log2_size + 1;

    a = PIXEL_SPLAT_X4(dc);

    for (i = 0; i < size; i++)
422 423
        for (j = 0; j < size; j+=4)
            AV_WN4P(&POS(j, i), a);
G
Guillaume Martres 已提交
424 425

    if (c_idx == 0 && size < 32) {
426
        POS(0, 0) = (left[0] + 2 * dc + top[0] + 2) >> 2;
G
Guillaume Martres 已提交
427 428 429 430 431 432 433 434 435 436 437 438 439 440
        for (x = 1; x < size; x++)
            POS(x, 0) = (top[x] + 3 * dc + 2) >> 2;
        for (y = 1; y < size; y++)
            POS(0, y) = (left[y] + 3 * dc + 2) >> 2;
    }
}

static av_always_inline void FUNC(pred_angular)(uint8_t *_src,
                                                const uint8_t *_top,
                                                const uint8_t *_left,
                                                ptrdiff_t stride, int c_idx,
                                                int mode, int size)
{
    int x, y;
441 442 443
    pixel *src        = (pixel *)_src;
    const pixel *top  = (const pixel *)_top;
    const pixel *left = (const pixel *)_left;
G
Guillaume Martres 已提交
444 445

    static const int intra_pred_angle[] = {
446 447
         32,  26,  21,  17, 13,  9,  5, 2, 0, -2, -5, -9, -13, -17, -21, -26, -32,
        -26, -21, -17, -13, -9, -5, -2, 0, 2,  5,  9, 13,  17,  21,  26,  32
G
Guillaume Martres 已提交
448 449 450 451 452 453 454
    };
    static const int inv_angle[] = {
        -4096, -1638, -910, -630, -482, -390, -315, -256, -315, -390, -482,
        -630, -910, -1638, -4096
    };

    int angle = intra_pred_angle[mode - 2];
455
    pixel ref_array[3 * MAX_TB_SIZE + 4];
G
Guillaume Martres 已提交
456 457 458 459 460 461 462
    pixel *ref_tmp = ref_array + size;
    const pixel *ref;
    int last = (size * angle) >> 5;

    if (mode >= 18) {
        ref = top - 1;
        if (angle < 0 && last < -1) {
463 464
            for (x = 0; x <= size; x += 4)
                AV_WN4P(&ref_tmp[x], AV_RN4P(&top[x - 1]));
G
Guillaume Martres 已提交
465
            for (x = last; x <= -1; x++)
466
                ref_tmp[x] = left[-1 + ((x * inv_angle[mode - 11] + 128) >> 8)];
G
Guillaume Martres 已提交
467 468 469 470 471 472 473
            ref = ref_tmp;
        }

        for (y = 0; y < size; y++) {
            int idx  = ((y + 1) * angle) >> 5;
            int fact = ((y + 1) * angle) & 31;
            if (fact) {
474
                for (x = 0; x < size; x += 4) {
G
Guillaume Martres 已提交
475 476
                    POS(x, y) = ((32 - fact) * ref[x + idx + 1] +
                                       fact  * ref[x + idx + 2] + 16) >> 5;
477 478 479 480 481 482
                    POS(x + 1, y) = ((32 - fact) * ref[x + 1 + idx + 1] +
                                 fact  * ref[x + 1 + idx + 2] + 16) >> 5;
                    POS(x + 2, y) = ((32 - fact) * ref[x + 2 + idx + 1] +
                                     fact  * ref[x + 2 + idx + 2] + 16) >> 5;
                    POS(x + 3, y) = ((32 - fact) * ref[x + 3 + idx + 1] +
                                     fact  * ref[x + 3 + idx + 2] + 16) >> 5;
G
Guillaume Martres 已提交
483 484
                }
            } else {
485 486
                for (x = 0; x < size; x += 4)
                    AV_WN4P(&POS(x, y), AV_RN4P(&ref[x + idx + 1]));
G
Guillaume Martres 已提交
487 488 489 490 491 492 493 494 495
            }
        }
        if (mode == 26 && c_idx == 0 && size < 32) {
            for (y = 0; y < size; y++)
                POS(0, y) = av_clip_pixel(top[0] + ((left[y] - left[-1]) >> 1));
        }
    } else {
        ref = left - 1;
        if (angle < 0 && last < -1) {
496 497
            for (x = 0; x <= size; x += 4)
                AV_WN4P(&ref_tmp[x], AV_RN4P(&left[x - 1]));
G
Guillaume Martres 已提交
498
            for (x = last; x <= -1; x++)
499
                ref_tmp[x] = top[-1 + ((x * inv_angle[mode - 11] + 128) >> 8)];
G
Guillaume Martres 已提交
500 501 502 503 504 505 506 507 508 509 510 511
            ref = ref_tmp;
        }

        for (x = 0; x < size; x++) {
            int idx  = ((x + 1) * angle) >> 5;
            int fact = ((x + 1) * angle) & 31;
            if (fact) {
                for (y = 0; y < size; y++) {
                    POS(x, y) = ((32 - fact) * ref[y + idx + 1] +
                                       fact  * ref[y + idx + 2] + 16) >> 5;
                }
            } else {
512
                for (y = 0; y < size; y++)
G
Guillaume Martres 已提交
513 514 515 516
                    POS(x, y) = ref[y + idx + 1];
            }
        }
        if (mode == 10 && c_idx == 0 && size < 32) {
517
            for (x = 0; x < size; x += 4) {
G
Guillaume Martres 已提交
518
                POS(x, 0) = av_clip_pixel(left[0] + ((top[x] - top[-1]) >> 1));
519 520 521 522
                POS(x+1, 0) = av_clip_pixel(left[0] + ((top[x+1] - top[-1]) >> 1));
                POS(x+2, 0) = av_clip_pixel(left[0] + ((top[x+2] - top[-1]) >> 1));
                POS(x+3, 0) = av_clip_pixel(left[0] + ((top[x+3] - top[-1]) >> 1));
            }
G
Guillaume Martres 已提交
523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553
        }
    }
}

static void FUNC(pred_angular_0)(uint8_t *src, const uint8_t *top,
                                 const uint8_t *left,
                                 ptrdiff_t stride, int c_idx, int mode)
{
    FUNC(pred_angular)(src, top, left, stride, c_idx, mode, 1 << 2);
}

static void FUNC(pred_angular_1)(uint8_t *src, const uint8_t *top,
                                 const uint8_t *left,
                                 ptrdiff_t stride, int c_idx, int mode)
{
    FUNC(pred_angular)(src, top, left, stride, c_idx, mode, 1 << 3);
}

static void FUNC(pred_angular_2)(uint8_t *src, const uint8_t *top,
                                 const uint8_t *left,
                                 ptrdiff_t stride, int c_idx, int mode)
{
    FUNC(pred_angular)(src, top, left, stride, c_idx, mode, 1 << 4);
}

static void FUNC(pred_angular_3)(uint8_t *src, const uint8_t *top,
                                 const uint8_t *left,
                                 ptrdiff_t stride, int c_idx, int mode)
{
    FUNC(pred_angular)(src, top, left, stride, c_idx, mode, 1 << 5);
}
554

555 556 557 558 559 560 561 562
#undef EXTEND_LEFT_CIP
#undef EXTEND_RIGHT_CIP
#undef EXTEND_UP_CIP
#undef EXTEND_DOWN_CIP
#undef IS_INTRA
#undef MVF_PU
#undef MVF
#undef PU
563
#undef EXTEND
564
#undef MIN_TB_ADDR_ZS
G
Guillaume Martres 已提交
565
#undef POS