aboutsummaryrefslogtreecommitdiffstats
path: root/libavcodec/loongarch/h264_cabac.c
blob: d88743bed7a427f7734435c77207f63ff87f16b2 (plain) (blame)
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
 * Loongson  optimized cabac
 *
 * Copyright (c) 2021 Loongson Technology Corporation Limited
 * Contributed by Hao Chen <chenhao@loongson.cn>
 *
 * 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 "libavcodec/cabac.h"
#include "cabac.h"

#define decode_significance decode_significance_loongarch
static int decode_significance_loongarch(CABACContext *c, int max_coeff,
    uint8_t *significant_coeff_ctx_base, int *index, int64_t last_off)
{
    void *end = significant_coeff_ctx_base + max_coeff - 1;
    int64_t minusstart = -(int64_t)significant_coeff_ctx_base;
    int64_t minusindex = 4 - (int64_t)index;
    int64_t bit, tmp0, tmp1, tmp2, one = 1;
    uint8_t *state = significant_coeff_ctx_base;

    __asm__ volatile(
    "3:"
#if UNCHECKED_BITSTREAM_READER
    GET_CABAC_LOONGARCH_UNCBSR
#else
    GET_CABAC_LOONGARCH
#endif
    "blt     %[bit],          %[one],            4f               \n\t"
    "add.d   %[state],        %[state],          %[last_off]      \n\t"
#if UNCHECKED_BITSTREAM_READER
    GET_CABAC_LOONGARCH_UNCBSR
#else
    GET_CABAC_LOONGARCH
#endif
    "sub.d   %[state],        %[state],          %[last_off]      \n\t"
    "add.d   %[tmp0],         %[state],          %[minusstart]    \n\t"
    "st.w    %[tmp0],         %[index],          0                \n\t"
    "bge     %[bit],          %[one],            5f               \n\t"
    "addi.d  %[index],        %[index],          4                \n\t"
    "4:                                                           \n\t"
    "addi.d  %[state],        %[state],          1                \n\t"
    "blt     %[state],        %[end],            3b               \n\t"
    "add.d   %[tmp0],         %[state],          %[minusstart]    \n\t"
    "st.w    %[tmp0],         %[index],          0                \n\t"
    "5:                                                           \n\t"
    "add.d   %[tmp0],         %[index],          %[minusindex]    \n\t"
    "srli.d  %[tmp0],         %[tmp0],           2                \n\t"
    : [bit]"=&r"(bit), [tmp0]"=&r"(tmp0), [tmp1]"=&r"(tmp1), [tmp2]"=&r"(tmp2),
      [c_range]"+&r"(c->range), [c_low]"+&r"(c->low), [state]"+&r"(state),
      [c_bytestream]"+&r"(c->bytestream), [index]"+&r"(index)
    : [tables]"r"(ff_h264_cabac_tables), [end]"r"(end), [one]"r"(one),
      [minusstart]"r"(minusstart), [minusindex]"r"(minusindex),
      [last_off]"r"(last_off),
#if !UNCHECKED_BITSTREAM_READER
      [c_bytestream_end]"r"(c->bytestream_end),
#endif
      [lps_off]"i"(H264_LPS_RANGE_OFFSET),
      [mlps_off]"i"(H264_MLPS_STATE_OFFSET + 128),
      [norm_off]"i"(H264_NORM_SHIFT_OFFSET),
      [cabac_mask]"r"(CABAC_MASK)
    : "memory"
    );

    return (int)tmp0;
}

#define decode_significance_8x8 decode_significance_8x8_loongarch
static int decode_significance_8x8_loongarch(
    CABACContext *c, uint8_t *significant_coeff_ctx_base,
    int *index, uint8_t *last_coeff_ctx_base, const uint8_t *sig_off)
{
    int64_t minusindex = 4 - (int64_t)index;
    int64_t bit, tmp0, tmp1, tmp2, one = 1, end =  63, last = 0;
    uint8_t *state = 0;
    int64_t flag_offset = H264_LAST_COEFF_FLAG_OFFSET_8x8_OFFSET;

    __asm__ volatile(
    "3:                                                              \n\t"
    "ldx.bu   %[tmp0],     %[sig_off],       %[last]                 \n\t"
    "add.d    %[state],    %[tmp0], %[significant_coeff_ctx_base]    \n\t"
#if UNCHECKED_BITSTREAM_READER
    GET_CABAC_LOONGARCH_UNCBSR
#else
    GET_CABAC_LOONGARCH
#endif
    "blt      %[bit],      %[one],           4f                      \n\t"
    "add.d    %[tmp0],     %[tables],        %[flag_offset]          \n\t"
    "ldx.bu   %[tmp1],     %[tmp0],          %[last]                 \n\t"
    "add.d    %[state],    %[tmp1],    %[last_coeff_ctx_base]        \n\t"
#if UNCHECKED_BITSTREAM_READER
    GET_CABAC_LOONGARCH_UNCBSR
#else
    GET_CABAC_LOONGARCH
#endif
    "st.w    %[last],      %[index],         0                       \n\t"
    "bge     %[bit],       %[one],           5f                      \n\t"
    "addi.d  %[index],     %[index],         4                       \n\t"
    "4:                                                              \n\t"
    "addi.d  %[last],      %[last],          1                       \n\t"
    "blt     %[last],      %[end],           3b                      \n\t"
    "st.w    %[last],      %[index],         0                       \n\t"
    "5:                                                              \n\t"
    "add.d   %[tmp0],      %[index],         %[minusindex]           \n\t"
    "srli.d  %[tmp0],      %[tmp0],          2                       \n\t"
    : [bit]"=&r"(bit), [tmp0]"=&r"(tmp0), [tmp1]"=&r"(tmp1),
      [tmp2]"=&r"(tmp2), [c_range]"+&r"(c->range),
      [c_low]"+&r"(c->low), [state]"+&r"(state), [last]"+&r"(last),
      [c_bytestream]"+&r"(c->bytestream), [index]"+&r"(index)
    : [tables]"r"(ff_h264_cabac_tables), [end]"r"(end),
      [one]"r"(one), [minusindex]"r"(minusindex),
      [last_coeff_ctx_base]"r"(last_coeff_ctx_base),
      [flag_offset]"r"(flag_offset),
#if !UNCHECKED_BITSTREAM_READER
      [c_bytestream_end]"r"(c->bytestream_end),
#endif
      [lps_off]"i"(H264_LPS_RANGE_OFFSET), [sig_off]"r"(sig_off),
      [mlps_off]"i"(H264_MLPS_STATE_OFFSET + 128),
      [norm_off]"i"(H264_NORM_SHIFT_OFFSET),
      [cabac_mask]"r"(CABAC_MASK),
      [significant_coeff_ctx_base]"r"(significant_coeff_ctx_base)
    );

    return (int)tmp0;
}