tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

vp9lpf.c (8979B)


      1 /*
      2 * VP9 compatible video decoder
      3 *
      4 * Copyright (C) 2013 Ronald S. Bultje <rsbultje gmail com>
      5 * Copyright (C) 2013 Clément Bœsch <u pkh me>
      6 *
      7 * This file is part of FFmpeg.
      8 *
      9 * FFmpeg is free software; you can redistribute it and/or
     10 * modify it under the terms of the GNU Lesser General Public
     11 * License as published by the Free Software Foundation; either
     12 * version 2.1 of the License, or (at your option) any later version.
     13 *
     14 * FFmpeg is distributed in the hope that it will be useful,
     15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
     16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     17 * Lesser General Public License for more details.
     18 *
     19 * You should have received a copy of the GNU Lesser General Public
     20 * License along with FFmpeg; if not, write to the Free Software
     21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
     22 */
     23 
     24 #include "avcodec.h"
     25 #include "vp9dec.h"
     26 
     27 static av_always_inline void filter_plane_cols(VP9Context *s, int col, int ss_h, int ss_v,
     28                                               uint8_t *lvl, uint8_t (*mask)[4],
     29                                               uint8_t *dst, ptrdiff_t ls)
     30 {
     31    int y, x, bytesperpixel = s->bytesperpixel;
     32 
     33    // filter edges between columns (e.g. block1 | block2)
     34    for (y = 0; y < 8; y += 2 << ss_v, dst += 16 * ls, lvl += 16 << ss_v) {
     35        uint8_t *ptr = dst, *l = lvl, *hmask1 = mask[y], *hmask2 = mask[y + 1 + ss_v];
     36        unsigned hm1 = hmask1[0] | hmask1[1] | hmask1[2], hm13 = hmask1[3];
     37        unsigned hm2 = hmask2[1] | hmask2[2], hm23 = hmask2[3];
     38        unsigned hm = hm1 | hm2 | hm13 | hm23;
     39 
     40        for (x = 1; hm & ~(x - 1); x <<= 1, ptr += 8 * bytesperpixel >> ss_h) {
     41            if (col || x > 1) {
     42                if (hm1 & x) {
     43                    int L = *l, H = L >> 4;
     44                    int E = s->filter_lut.mblim_lut[L], I = s->filter_lut.lim_lut[L];
     45 
     46                    if (hmask1[0] & x) {
     47                        if (hmask2[0] & x) {
     48                            av_assert2(l[8 << ss_v] == L);
     49                            s->dsp.loop_filter_16[0](ptr, ls, E, I, H);
     50                        } else {
     51                            s->dsp.loop_filter_8[2][0](ptr, ls, E, I, H);
     52                        }
     53                    } else if (hm2 & x) {
     54                        L = l[8 << ss_v];
     55                        H |= (L >> 4) << 8;
     56                        E |= s->filter_lut.mblim_lut[L] << 8;
     57                        I |= s->filter_lut.lim_lut[L] << 8;
     58                        s->dsp.loop_filter_mix2[!!(hmask1[1] & x)]
     59                                               [!!(hmask2[1] & x)]
     60                                               [0](ptr, ls, E, I, H);
     61                    } else {
     62                        s->dsp.loop_filter_8[!!(hmask1[1] & x)]
     63                                            [0](ptr, ls, E, I, H);
     64                    }
     65                } else if (hm2 & x) {
     66                    int L = l[8 << ss_v], H = L >> 4;
     67                    int E = s->filter_lut.mblim_lut[L], I = s->filter_lut.lim_lut[L];
     68 
     69                    s->dsp.loop_filter_8[!!(hmask2[1] & x)]
     70                                        [0](ptr + 8 * ls, ls, E, I, H);
     71                }
     72            }
     73            if (ss_h) {
     74                if (x & 0xAA)
     75                    l += 2;
     76            } else {
     77                if (hm13 & x) {
     78                    int L = *l, H = L >> 4;
     79                    int E = s->filter_lut.mblim_lut[L], I = s->filter_lut.lim_lut[L];
     80 
     81                    if (hm23 & x) {
     82                        L = l[8 << ss_v];
     83                        H |= (L >> 4) << 8;
     84                        E |= s->filter_lut.mblim_lut[L] << 8;
     85                        I |= s->filter_lut.lim_lut[L] << 8;
     86                        s->dsp.loop_filter_mix2[0][0][0](ptr + 4 * bytesperpixel, ls, E, I, H);
     87                    } else {
     88                        s->dsp.loop_filter_8[0][0](ptr + 4 * bytesperpixel, ls, E, I, H);
     89                    }
     90                } else if (hm23 & x) {
     91                    int L = l[8 << ss_v], H = L >> 4;
     92                    int E = s->filter_lut.mblim_lut[L], I = s->filter_lut.lim_lut[L];
     93 
     94                    s->dsp.loop_filter_8[0][0](ptr + 8 * ls + 4 * bytesperpixel, ls, E, I, H);
     95                }
     96                l++;
     97            }
     98        }
     99    }
    100 }
    101 
    102 static av_always_inline void filter_plane_rows(VP9Context *s, int row, int ss_h, int ss_v,
    103                                               uint8_t *lvl, uint8_t (*mask)[4],
    104                                               uint8_t *dst, ptrdiff_t ls)
    105 {
    106    int y, x, bytesperpixel = s->bytesperpixel;
    107 
    108    //                                 block1
    109    // filter edges between rows (e.g. ------)
    110    //                                 block2
    111    for (y = 0; y < 8; y++, dst += 8 * ls >> ss_v) {
    112        uint8_t *ptr = dst, *l = lvl, *vmask = mask[y];
    113        unsigned vm = vmask[0] | vmask[1] | vmask[2], vm3 = vmask[3];
    114 
    115        for (x = 1; vm & ~(x - 1); x <<= (2 << ss_h), ptr += 16 * bytesperpixel, l += 2 << ss_h) {
    116            if (row || y) {
    117                if (vm & x) {
    118                    int L = *l, H = L >> 4;
    119                    int E = s->filter_lut.mblim_lut[L], I = s->filter_lut.lim_lut[L];
    120 
    121                    if (vmask[0] & x) {
    122                        if (vmask[0] & (x << (1 + ss_h))) {
    123                            av_assert2(l[1 + ss_h] == L);
    124                            s->dsp.loop_filter_16[1](ptr, ls, E, I, H);
    125                        } else {
    126                            s->dsp.loop_filter_8[2][1](ptr, ls, E, I, H);
    127                        }
    128                    } else if (vm & (x << (1 + ss_h))) {
    129                        L = l[1 + ss_h];
    130                        H |= (L >> 4) << 8;
    131                        E |= s->filter_lut.mblim_lut[L] << 8;
    132                        I |= s->filter_lut.lim_lut[L] << 8;
    133                        s->dsp.loop_filter_mix2[!!(vmask[1] &  x)]
    134                                               [!!(vmask[1] & (x << (1 + ss_h)))]
    135                                               [1](ptr, ls, E, I, H);
    136                    } else {
    137                        s->dsp.loop_filter_8[!!(vmask[1] & x)]
    138                                            [1](ptr, ls, E, I, H);
    139                    }
    140                } else if (vm & (x << (1 + ss_h))) {
    141                    int L = l[1 + ss_h], H = L >> 4;
    142                    int E = s->filter_lut.mblim_lut[L], I = s->filter_lut.lim_lut[L];
    143 
    144                    s->dsp.loop_filter_8[!!(vmask[1] & (x << (1 + ss_h)))]
    145                                        [1](ptr + 8 * bytesperpixel, ls, E, I, H);
    146                }
    147            }
    148            if (!ss_v) {
    149                if (vm3 & x) {
    150                    int L = *l, H = L >> 4;
    151                    int E = s->filter_lut.mblim_lut[L], I = s->filter_lut.lim_lut[L];
    152 
    153                    if (vm3 & (x << (1 + ss_h))) {
    154                        L = l[1 + ss_h];
    155                        H |= (L >> 4) << 8;
    156                        E |= s->filter_lut.mblim_lut[L] << 8;
    157                        I |= s->filter_lut.lim_lut[L] << 8;
    158                        s->dsp.loop_filter_mix2[0][0][1](ptr + ls * 4, ls, E, I, H);
    159                    } else {
    160                        s->dsp.loop_filter_8[0][1](ptr + ls * 4, ls, E, I, H);
    161                    }
    162                } else if (vm3 & (x << (1 + ss_h))) {
    163                    int L = l[1 + ss_h], H = L >> 4;
    164                    int E = s->filter_lut.mblim_lut[L], I = s->filter_lut.lim_lut[L];
    165 
    166                    s->dsp.loop_filter_8[0][1](ptr + ls * 4 + 8 * bytesperpixel, ls, E, I, H);
    167                }
    168            }
    169        }
    170        if (ss_v) {
    171            if (y & 1)
    172                lvl += 16;
    173        } else {
    174            lvl += 8;
    175        }
    176    }
    177 }
    178 
    179 void ff_vp9_loopfilter_sb(AVCodecContext *avctx, VP9Filter *lflvl,
    180                          int row, int col, ptrdiff_t yoff, ptrdiff_t uvoff)
    181 {
    182    VP9Context *s = avctx->priv_data;
    183    AVFrame *f = s->s.frames[CUR_FRAME].tf.f;
    184    uint8_t *dst = f->data[0] + yoff;
    185    ptrdiff_t ls_y = f->linesize[0], ls_uv = f->linesize[1];
    186    uint8_t (*uv_masks)[8][4] = lflvl->mask[s->ss_h | s->ss_v];
    187    int p;
    188 
    189    /* FIXME: In how far can we interleave the v/h loopfilter calls? E.g.
    190     * if you think of them as acting on a 8x8 block max, we can interleave
    191     * each v/h within the single x loop, but that only works if we work on
    192     * 8 pixel blocks, and we won't always do that (we want at least 16px
    193     * to use SSE2 optimizations, perhaps 32 for AVX2) */
    194 
    195    filter_plane_cols(s, col, 0, 0, lflvl->level, lflvl->mask[0][0], dst, ls_y);
    196    filter_plane_rows(s, row, 0, 0, lflvl->level, lflvl->mask[0][1], dst, ls_y);
    197 
    198    for (p = 0; p < 2; p++) {
    199        dst = f->data[1 + p] + uvoff;
    200        filter_plane_cols(s, col, s->ss_h, s->ss_v, lflvl->level, uv_masks[0], dst, ls_uv);
    201        filter_plane_rows(s, row, s->ss_h, s->ss_v, lflvl->level, uv_masks[1], dst, ls_uv);
    202    }
    203 }