1 /*
2  * Loongson SIMD optimized h264chroma
3  *
4  * Copyright (c) 2018 Loongson Technology Corporation Limited
5  * Copyright (c) 2018 Shiyou Yin <yinshiyou-hf@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 #ifndef AVCODEC_MIPS_CABAC_H
25 #define AVCODEC_MIPS_CABAC_H
26 
27 #include "libavcodec/cabac.h"
28 #include "libavutil/mips/asmdefs.h"
29 #include "config.h"
30 
31 #define get_cabac_inline get_cabac_inline_mips
get_cabac_inline_mips(CABACContext * c,uint8_t * const state)32 static av_always_inline int get_cabac_inline_mips(CABACContext *c,
33                                              uint8_t * const state){
34     mips_reg tmp0, tmp1, tmp2, bit;
35 
36     __asm__ volatile (
37         "lbu          %[bit],        0(%[state])                   \n\t"
38         "and          %[tmp0],       %[c_range],     0xC0          \n\t"
39         PTR_ADDU     "%[tmp0],       %[tmp0],        %[tmp0]       \n\t"
40         PTR_ADDU     "%[tmp0],       %[tmp0],        %[tables]     \n\t"
41         PTR_ADDU     "%[tmp0],       %[tmp0],        %[bit]        \n\t"
42         /* tmp1: RangeLPS */
43         "lbu          %[tmp1],       %[lps_off](%[tmp0])           \n\t"
44 
45         PTR_SUBU     "%[c_range],    %[c_range],     %[tmp1]       \n\t"
46         PTR_SLL      "%[tmp0],       %[c_range],     0x11          \n\t"
47         PTR_SUBU     "%[tmp0],       %[tmp0],        %[c_low]      \n\t"
48 
49         /* tmp2: lps_mask */
50         PTR_SRA      "%[tmp2],       %[tmp0],        0x1F          \n\t"
51         /* If tmp0 < 0, lps_mask ==  0xffffffff*/
52         /* If tmp0 >= 0, lps_mask ==  0x00000000*/
53         "beqz         %[tmp2],       1f                            \n\t"
54         PTR_SLL      "%[tmp0],       %[c_range],     0x11          \n\t"
55         PTR_SUBU     "%[c_low],      %[c_low],       %[tmp0]       \n\t"
56         PTR_SUBU     "%[tmp0],       %[tmp1],        %[c_range]    \n\t"
57         PTR_ADDU     "%[c_range],    %[c_range],     %[tmp0]       \n\t"
58         "xor          %[bit],        %[bit],         %[tmp2]       \n\t"
59 
60         "1:                                                        \n\t"
61         /* tmp1: *state */
62         PTR_ADDU     "%[tmp0],       %[tables],      %[bit]        \n\t"
63         "lbu          %[tmp1],       %[mlps_off](%[tmp0])          \n\t"
64         /* tmp2: lps_mask */
65         PTR_ADDU     "%[tmp0],       %[tables],      %[c_range]    \n\t"
66         "lbu          %[tmp2],       %[norm_off](%[tmp0])          \n\t"
67 
68         "sb           %[tmp1],       0(%[state])                   \n\t"
69         "and          %[bit],        %[bit],         0x01          \n\t"
70         PTR_SLL      "%[c_range],    %[c_range],     %[tmp2]       \n\t"
71         PTR_SLL      "%[c_low],      %[c_low],       %[tmp2]       \n\t"
72 
73         "and          %[tmp0],       %[c_low],       %[cabac_mask] \n\t"
74         "bnez         %[tmp0],       1f                            \n\t"
75         PTR_ADDIU    "%[tmp0],       %[c_low],       -0x01         \n\t"
76         "xor          %[tmp0],       %[c_low],       %[tmp0]       \n\t"
77         PTR_SRA      "%[tmp0],       %[tmp0],        0x0f          \n\t"
78         PTR_ADDU     "%[tmp0],       %[tmp0],        %[tables]     \n\t"
79         "lbu          %[tmp2],       %[norm_off](%[tmp0])          \n\t"
80 #if CABAC_BITS == 16
81         "lbu          %[tmp0],       0(%[c_bytestream])            \n\t"
82         "lbu          %[tmp1],       1(%[c_bytestream])            \n\t"
83         PTR_SLL      "%[tmp0],       %[tmp0],        0x09          \n\t"
84         PTR_SLL      "%[tmp1],       %[tmp1],        0x01          \n\t"
85         PTR_ADDU     "%[tmp0],       %[tmp0],        %[tmp1]       \n\t"
86 #else
87         "lbu          %[tmp0],       0(%[c_bytestream])            \n\t"
88         PTR_SLL      "%[tmp0],       %[tmp0],        0x01          \n\t"
89 #endif
90         PTR_SUBU     "%[tmp0],       %[tmp0],        %[cabac_mask] \n\t"
91 
92         "li           %[tmp1],       0x07                          \n\t"
93         PTR_SUBU     "%[tmp1],       %[tmp1],        %[tmp2]       \n\t"
94         PTR_SLL      "%[tmp0],       %[tmp0],        %[tmp1]       \n\t"
95         PTR_ADDU     "%[c_low],      %[c_low],       %[tmp0]       \n\t"
96 
97 #if !UNCHECKED_BITSTREAM_READER
98         "bge          %[c_bytestream], %[c_bytestream_end], 1f     \n\t"
99 #endif
100         PTR_ADDIU    "%[c_bytestream], %[c_bytestream],     0X02   \n\t"
101         "1:                                                        \n\t"
102     : [bit]"=&r"(bit), [tmp0]"=&r"(tmp0), [tmp1]"=&r"(tmp1), [tmp2]"=&r"(tmp2),
103       [c_range]"+&r"(c->range), [c_low]"+&r"(c->low),
104       [c_bytestream]"+&r"(c->bytestream)
105     : [state]"r"(state), [tables]"r"(ff_h264_cabac_tables),
106 #if !UNCHECKED_BITSTREAM_READER
107       [c_bytestream_end]"r"(c->bytestream_end),
108 #endif
109       [lps_off]"i"(H264_LPS_RANGE_OFFSET),
110       [mlps_off]"i"(H264_MLPS_STATE_OFFSET + 128),
111       [norm_off]"i"(H264_NORM_SHIFT_OFFSET),
112       [cabac_mask]"r"(CABAC_MASK)
113     : "memory"
114     );
115 
116     return bit;
117 }
118 
119 #endif /* AVCODEC_MIPS_CABAC_H */
120