FFmpeg
h264_cabac.c
Go to the documentation of this file.
1 /*
2  * Loongson optimized cabac
3  *
4  * Copyright (c) 2021 Loongson Technology Corporation Limited
5  * Contributed by Hao Chen <chenhao@loongson.cn>
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 "libavcodec/cabac.h"
25 #include "cabac.h"
26 
27 #define decode_significance decode_significance_loongarch
28 static int decode_significance_loongarch(CABACContext *c, int max_coeff,
29  uint8_t *significant_coeff_ctx_base, int *index, int64_t last_off)
30 {
31  void *end = significant_coeff_ctx_base + max_coeff - 1;
32  int64_t minusstart = -(int64_t)significant_coeff_ctx_base;
33  int64_t minusindex = 4 - (int64_t)index;
34  int64_t bit, tmp0, tmp1, tmp2, one = 1;
35  uint8_t *state = significant_coeff_ctx_base;
36 
37  __asm__ volatile(
38  "3:"
39 #if UNCHECKED_BITSTREAM_READER
41 #else
43 #endif
44  "blt %[bit], %[one], 4f \n\t"
45  "add.d %[state], %[state], %[last_off] \n\t"
46 #if UNCHECKED_BITSTREAM_READER
48 #else
50 #endif
51  "sub.d %[state], %[state], %[last_off] \n\t"
52  "add.d %[tmp0], %[state], %[minusstart] \n\t"
53  "st.w %[tmp0], %[index], 0 \n\t"
54  "bge %[bit], %[one], 5f \n\t"
55  "addi.d %[index], %[index], 4 \n\t"
56  "4: \n\t"
57  "addi.d %[state], %[state], 1 \n\t"
58  "blt %[state], %[end], 3b \n\t"
59  "add.d %[tmp0], %[state], %[minusstart] \n\t"
60  "st.w %[tmp0], %[index], 0 \n\t"
61  "5: \n\t"
62  "add.d %[tmp0], %[index], %[minusindex] \n\t"
63  "srli.d %[tmp0], %[tmp0], 2 \n\t"
64  : [bit]"=&r"(bit), [tmp0]"=&r"(tmp0), [tmp1]"=&r"(tmp1), [tmp2]"=&r"(tmp2),
65  [c_range]"+&r"(c->range), [c_low]"+&r"(c->low), [state]"+&r"(state),
66  [c_bytestream]"+&r"(c->bytestream), [index]"+&r"(index)
67  : [tables]"r"(ff_h264_cabac_tables), [end]"r"(end), [one]"r"(one),
68  [minusstart]"r"(minusstart), [minusindex]"r"(minusindex),
69  [last_off]"r"(last_off),
71  [c_bytestream_end]"r"(c->bytestream_end),
72 #endif
73  [lps_off]"i"(H264_LPS_RANGE_OFFSET),
74  [mlps_off]"i"(H264_MLPS_STATE_OFFSET + 128),
75  [norm_off]"i"(H264_NORM_SHIFT_OFFSET),
76  [cabac_mask]"r"(CABAC_MASK)
77  : "memory"
78  );
79 
80  return (int)tmp0;
81 }
82 
83 #define decode_significance_8x8 decode_significance_8x8_loongarch
85  CABACContext *c, uint8_t *significant_coeff_ctx_base,
86  int *index, uint8_t *last_coeff_ctx_base, const uint8_t *sig_off)
87 {
88  int64_t minusindex = 4 - (int64_t)index;
89  int64_t bit, tmp0, tmp1, tmp2, one = 1, end = 63, last = 0;
90  uint8_t *state = 0;
91  int64_t flag_offset = H264_LAST_COEFF_FLAG_OFFSET_8x8_OFFSET;
92 
93  __asm__ volatile(
94  "3: \n\t"
95  "ldx.bu %[tmp0], %[sig_off], %[last] \n\t"
96  "add.d %[state], %[tmp0], %[significant_coeff_ctx_base] \n\t"
97 #if UNCHECKED_BITSTREAM_READER
99 #else
101 #endif
102  "blt %[bit], %[one], 4f \n\t"
103  "add.d %[tmp0], %[tables], %[flag_offset] \n\t"
104  "ldx.bu %[tmp1], %[tmp0], %[last] \n\t"
105  "add.d %[state], %[tmp1], %[last_coeff_ctx_base] \n\t"
106 #if UNCHECKED_BITSTREAM_READER
108 #else
110 #endif
111  "st.w %[last], %[index], 0 \n\t"
112  "bge %[bit], %[one], 5f \n\t"
113  "addi.d %[index], %[index], 4 \n\t"
114  "4: \n\t"
115  "addi.d %[last], %[last], 1 \n\t"
116  "blt %[last], %[end], 3b \n\t"
117  "st.w %[last], %[index], 0 \n\t"
118  "5: \n\t"
119  "add.d %[tmp0], %[index], %[minusindex] \n\t"
120  "srli.d %[tmp0], %[tmp0], 2 \n\t"
121  : [bit]"=&r"(bit), [tmp0]"=&r"(tmp0), [tmp1]"=&r"(tmp1),
122  [tmp2]"=&r"(tmp2), [c_range]"+&r"(c->range),
123  [c_low]"+&r"(c->low), [state]"+&r"(state), [last]"+&r"(last),
124  [c_bytestream]"+&r"(c->bytestream), [index]"+&r"(index)
125  : [tables]"r"(ff_h264_cabac_tables), [end]"r"(end),
126  [one]"r"(one), [minusindex]"r"(minusindex),
127  [last_coeff_ctx_base]"r"(last_coeff_ctx_base),
128  [flag_offset]"r"(flag_offset),
129 #if !UNCHECKED_BITSTREAM_READER
130  [c_bytestream_end]"r"(c->bytestream_end),
131 #endif
132  [lps_off]"i"(H264_LPS_RANGE_OFFSET), [sig_off]"r"(sig_off),
133  [mlps_off]"i"(H264_MLPS_STATE_OFFSET + 128),
134  [norm_off]"i"(H264_NORM_SHIFT_OFFSET),
135  [cabac_mask]"r"(CABAC_MASK),
136  [significant_coeff_ctx_base]"r"(significant_coeff_ctx_base)
137  );
138 
139  return (int)tmp0;
140 }
GET_CABAC_LOONGARCH_UNCBSR
#define GET_CABAC_LOONGARCH_UNCBSR
Definition: cabac.h:31
cabac.h
index
fg index
Definition: ffmpeg_filter.c:167
bit
#define bit(string, value)
Definition: cbs_mpeg2.c:58
tables
Writing a table generator This documentation is preliminary Parts of the API are not good and should be changed Basic concepts A table generator consists of two *_tablegen c and *_tablegen h The h file will provide the variable declarations and initialization code for the tables
Definition: tablegen.txt:10
ff_h264_cabac_tables
const uint8_t ff_h264_cabac_tables[512+4 *2 *64+4 *64+63]
CABAC_MASK
#define CABAC_MASK
Definition: cabac.h:39
GET_CABAC_LOONGARCH
#define GET_CABAC_LOONGARCH
Definition: cabac.h:73
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)
Definition: h264_cabac.c:84
c
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
state
static struct @320 state
H264_LPS_RANGE_OFFSET
#define H264_LPS_RANGE_OFFSET
Definition: cabac.h:34
H264_NORM_SHIFT_OFFSET
#define H264_NORM_SHIFT_OFFSET
Definition: cabac.h:33
__asm__
__asm__(".macro parse_r var r\n\t" "\\var = -1\n\t" _IFC_REG(0) _IFC_REG(1) _IFC_REG(2) _IFC_REG(3) _IFC_REG(4) _IFC_REG(5) _IFC_REG(6) _IFC_REG(7) _IFC_REG(8) _IFC_REG(9) _IFC_REG(10) _IFC_REG(11) _IFC_REG(12) _IFC_REG(13) _IFC_REG(14) _IFC_REG(15) _IFC_REG(16) _IFC_REG(17) _IFC_REG(18) _IFC_REG(19) _IFC_REG(20) _IFC_REG(21) _IFC_REG(22) _IFC_REG(23) _IFC_REG(24) _IFC_REG(25) _IFC_REG(26) _IFC_REG(27) _IFC_REG(28) _IFC_REG(29) _IFC_REG(30) _IFC_REG(31) ".iflt \\var\n\t" ".error \"Unable to parse register name \\r\"\n\t" ".endif\n\t" ".endm")
UNCHECKED_BITSTREAM_READER
#define UNCHECKED_BITSTREAM_READER
Definition: h264_cabac.c:29
H264_LAST_COEFF_FLAG_OFFSET_8x8_OFFSET
#define H264_LAST_COEFF_FLAG_OFFSET_8x8_OFFSET
Definition: cabac.h:36
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)
Definition: h264_cabac.c:28
H264_MLPS_STATE_OFFSET
#define H264_MLPS_STATE_OFFSET
Definition: cabac.h:35
CABACContext
Definition: cabac.h:41