1 /*
2  * This file is part of FFmpeg.
3  *
4  * FFmpeg is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * FFmpeg is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with FFmpeg; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 #ifndef AVCODEC_ARM_CABAC_H
20 #define AVCODEC_ARM_CABAC_H
21 
22 #include "config.h"
23 #if HAVE_ARMV6T2_INLINE
24 
25 #include "libavutil/attributes.h"
26 #include "libavutil/internal.h"
27 #include "libavcodec/cabac.h"
28 
29 #define get_cabac_inline get_cabac_inline_arm
get_cabac_inline_arm(CABACContext * c,uint8_t * const state)30 static av_always_inline int get_cabac_inline_arm(CABACContext *c,
31                                                  uint8_t *const state)
32 {
33     int bit;
34     void *reg_b, *reg_c, *tmp;
35 
36     __asm__ volatile(
37         "ldrb       %[bit]        , [%[state]]                  \n\t"
38         "add        %[r_b]        , %[tables]   , %[lps_off]    \n\t"
39         "mov        %[tmp]        , %[range]                    \n\t"
40         "and        %[range]      , %[range]    , #0xC0         \n\t"
41         "add        %[r_b]        , %[r_b]      , %[bit]        \n\t"
42         "ldrb       %[range]      , [%[r_b], %[range], lsl #1]  \n\t"
43         "add        %[r_b]        , %[tables]   , %[norm_off]   \n\t"
44         "sub        %[r_c]        , %[tmp]      , %[range]      \n\t"
45         "lsl        %[tmp]        , %[r_c]      , #17           \n\t"
46         "cmp        %[tmp]        , %[low]                      \n\t"
47         "it         gt                                          \n\t"
48         "movgt      %[range]      , %[r_c]                      \n\t"
49         "itt        cc                                          \n\t"
50         "mvncc      %[bit]        , %[bit]                      \n\t"
51         "subcc      %[low]        , %[low]      , %[tmp]        \n\t"
52         "add        %[r_c]        , %[tables]   , %[mlps_off]   \n\t"
53         "ldrb       %[tmp]        , [%[r_b], %[range]]          \n\t"
54         "ldrb       %[r_b]        , [%[r_c], %[bit]]            \n\t"
55         "lsl        %[low]        , %[low]      , %[tmp]        \n\t"
56         "lsl        %[range]      , %[range]    , %[tmp]        \n\t"
57         "uxth       %[r_c]        , %[low]                      \n\t"
58         "strb       %[r_b]        , [%[state]]                  \n\t"
59         "tst        %[r_c]        , %[r_c]                      \n\t"
60         "bne        2f                                          \n\t"
61         "ldr        %[r_c]        , [%[c], %[byte]]             \n\t"
62 #if UNCHECKED_BITSTREAM_READER
63         "ldrh       %[tmp]        , [%[r_c]]                    \n\t"
64         "add        %[r_c]        , %[r_c]      , #2            \n\t"
65         "str        %[r_c]        , [%[c], %[byte]]             \n\t"
66 #else
67         "ldr        %[r_b]        , [%[c], %[end]]              \n\t"
68         "ldrh       %[tmp]        , [%[r_c]]                    \n\t"
69         "cmp        %[r_c]        , %[r_b]                      \n\t"
70         "itt        lt                                          \n\t"
71         "addlt      %[r_c]        , %[r_c]      , #2            \n\t"
72         "strlt      %[r_c]        , [%[c], %[byte]]             \n\t"
73 #endif
74         "sub        %[r_c]        , %[low]      , #1            \n\t"
75         "add        %[r_b]        , %[tables]   , %[norm_off]   \n\t"
76         "eor        %[r_c]        , %[low]      , %[r_c]        \n\t"
77         "rev        %[tmp]        , %[tmp]                      \n\t"
78         "lsr        %[r_c]        , %[r_c]      , #15           \n\t"
79         "lsr        %[tmp]        , %[tmp]      , #15           \n\t"
80         "ldrb       %[r_c]        , [%[r_b], %[r_c]]            \n\t"
81         "movw       %[r_b]        , #0xFFFF                     \n\t"
82         "sub        %[tmp]        , %[tmp]      , %[r_b]        \n\t"
83         "rsb        %[r_c]        , %[r_c]      , #7            \n\t"
84         "lsl        %[tmp]        , %[tmp]      , %[r_c]        \n\t"
85         "add        %[low]        , %[low]      , %[tmp]        \n\t"
86         "2:                                                     \n\t"
87         :    [bit]"=&r"(bit),
88              [low]"+&r"(c->low),
89            [range]"+&r"(c->range),
90              [r_b]"=&r"(reg_b),
91              [r_c]"=&r"(reg_c),
92              [tmp]"=&r"(tmp)
93         :        [c]"r"(c),
94              [state]"r"(state),
95             [tables]"r"(ff_h264_cabac_tables),
96               [byte]"M"(offsetof(CABACContext, bytestream)),
97                [end]"M"(offsetof(CABACContext, bytestream_end)),
98           [norm_off]"I"(H264_NORM_SHIFT_OFFSET),
99            [lps_off]"I"(H264_LPS_RANGE_OFFSET),
100           [mlps_off]"I"(H264_MLPS_STATE_OFFSET + 128)
101         : "memory", "cc"
102         );
103 
104     return bit & 1;
105 }
106 #endif /* HAVE_ARMV6T2_INLINE */
107 
108 #endif /* AVCODEC_ARM_CABAC_H */
109