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_AARCH64_CABAC_H
20#define AVCODEC_AARCH64_CABAC_H
21
22#include "config.h"
23#if HAVE_INLINE_ASM
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_aarch64
30static av_always_inline int get_cabac_inline_aarch64(CABACContext *c,
31                                                     uint8_t *const state)
32{
33    int bit;
34    void *reg_a, *reg_b, *reg_c, *tmp;
35
36    __asm__ volatile(
37        "ldrb       %w[bit]       , [%[state]]                  \n\t"
38        "add        %[r_b]        , %[tables]   , %[lps_off]    \n\t"
39        "mov        %w[tmp]       , %w[range]                   \n\t"
40        "and        %w[range]     , %w[range]   , #0xC0         \n\t"
41        "lsl        %w[r_c]       , %w[range]   , #1            \n\t"
42        "add        %[r_b]        , %[r_b]      , %w[bit], UXTW \n\t"
43        "ldrb       %w[range]     , [%[r_b], %w[r_c], SXTW]     \n\t"
44        "sub        %w[r_c]       , %w[tmp]     , %w[range]     \n\t"
45        "lsl        %w[tmp]       , %w[r_c]     , #17           \n\t"
46        "cmp        %w[tmp]       , %w[low]                     \n\t"
47        "csel       %w[tmp]       , %w[tmp]     , wzr      , cc \n\t"
48        "csel       %w[range]     , %w[r_c]     , %w[range], gt \n\t"
49        "cinv       %w[bit]       , %w[bit]     , cc            \n\t"
50        "sub        %w[low]       , %w[low]     , %w[tmp]       \n\t"
51        "add        %[r_b]        , %[tables]   , %[norm_off]   \n\t"
52        "add        %[r_a]        , %[tables]   , %[mlps_off]   \n\t"
53        "ldrb       %w[tmp]       , [%[r_b], %w[range], SXTW]   \n\t"
54        "ldrb       %w[r_a]       , [%[r_a], %w[bit], SXTW]     \n\t"
55        "lsl        %w[low]       , %w[low]     , %w[tmp]       \n\t"
56        "lsl        %w[range]     , %w[range]   , %w[tmp]       \n\t"
57        "uxth       %w[r_c]       , %w[low]                     \n\t"
58        "strb       %w[r_a]       , [%[state]]                  \n\t"
59        "cbnz       %w[r_c]       , 2f                          \n\t"
60        "ldr        %[r_c]        , [%[c], %[byte]]             \n\t"
61        "ldr        %[r_a]        , [%[c], %[end]]              \n\t"
62        "ldrh       %w[tmp]       , [%[r_c]]                    \n\t"
63        "cmp        %[r_c]        , %[r_a]                      \n\t"
64        "b.ge       1f                                          \n\t"
65        "add        %[r_a]        , %[r_c]      , #2            \n\t"
66        "str        %[r_a]        , [%[c], %[byte]]             \n\t"
67        "1:                                                     \n\t"
68        "sub        %w[r_c]       , %w[low]     , #1            \n\t"
69        "eor        %w[r_c]       , %w[r_c]     , %w[low]       \n\t"
70        "rev        %w[tmp]       , %w[tmp]                     \n\t"
71        "lsr        %w[r_c]       , %w[r_c]     , #15           \n\t"
72        "lsr        %w[tmp]       , %w[tmp]     , #15           \n\t"
73        "ldrb       %w[r_c]       , [%[r_b], %w[r_c], SXTW]     \n\t"
74        "mov        %w[r_b]       , #0xFFFF                     \n\t"
75        "mov        %w[r_a]       , #7                          \n\t"
76        "sub        %w[tmp]       , %w[tmp]     , %w[r_b]       \n\t"
77        "sub        %w[r_c]       , %w[r_a]     , %w[r_c]       \n\t"
78        "lsl        %w[tmp]       , %w[tmp]     , %w[r_c]       \n\t"
79        "add        %w[low]       , %w[low]     , %w[tmp]       \n\t"
80        "2:                                                     \n\t"
81        :    [bit]"=&r"(bit),
82             [low]"+&r"(c->low),
83           [range]"+&r"(c->range),
84             [r_a]"=&r"(reg_a),
85             [r_b]"=&r"(reg_b),
86             [r_c]"=&r"(reg_c),
87             [tmp]"=&r"(tmp)
88        :        [c]"r"(c),
89             [state]"r"(state),
90            [tables]"r"(ff_h264_cabac_tables),
91              [byte]"i"(offsetof(CABACContext, bytestream)),
92               [end]"i"(offsetof(CABACContext, bytestream_end)),
93          [norm_off]"I"(H264_NORM_SHIFT_OFFSET),
94           [lps_off]"I"(H264_LPS_RANGE_OFFSET),
95          [mlps_off]"I"(H264_MLPS_STATE_OFFSET + 128)
96        : "memory", "cc"
97        );
98
99    return bit & 1;
100}
101
102#endif /* HAVE_INLINE_ASM */
103
104#endif /* AVCODEC_AARCH64_CABAC_H */
105