aboutsummaryrefslogtreecommitdiffstats
path: root/libavcodec/aarch64/cabac.h
blob: e12953e86c8c5ee3181806a259754b3a5966bdff (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
/*
 * This file is part of Libav.
 *
 * Libav 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.
 *
 * Libav 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 Libav; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#ifndef AVCODEC_AARCH64_CABAC_H
#define AVCODEC_AARCH64_CABAC_H

#include "config.h"
#if HAVE_INLINE_ASM

#include "libavutil/attributes.h"
#include "libavutil/internal.h"
#include "libavcodec/cabac.h"

#define get_cabac_inline get_cabac_inline_aarch64
static av_always_inline int get_cabac_inline_aarch64(CABACContext *c,
                                                     uint8_t *const state)
{
    int bit;
    void *reg_a, *reg_b, *reg_c, *tmp;

    __asm__ volatile(
        "ldrb       %w[bit]       , [%[state]]                  \n\t"
        "add        %[r_b]        , %[tables]   , %[lps_off]    \n\t"
        "mov        %w[tmp]       , %w[range]                   \n\t"
        "and        %w[range]     , %w[range]   , #0xC0         \n\t"
        "lsl        %w[r_c]       , %w[range]   , #1            \n\t"
        "add        %[r_b]        , %[r_b]      , %w[bit], UXTW \n\t"
        "ldrb       %w[range]     , [%[r_b], %w[r_c], SXTW]     \n\t"
        "sub        %w[r_c]       , %w[tmp]     , %w[range]     \n\t"
        "lsl        %w[tmp]       , %w[r_c]     , #17           \n\t"
        "cmp        %w[tmp]       , %w[low]                     \n\t"
        "csel       %w[tmp]       , %w[tmp]     , wzr      , cc \n\t"
        "csel       %w[range]     , %w[r_c]     , %w[range], gt \n\t"
        "cinv       %w[bit]       , %w[bit]     , cc            \n\t"
        "sub        %w[low]       , %w[low]     , %w[tmp]       \n\t"
        "add        %[r_b]        , %[tables]   , %[norm_off]   \n\t"
        "add        %[r_a]        , %[tables]   , %[mlps_off]   \n\t"
        "ldrb       %w[tmp]       , [%[r_b], %w[range], SXTW]   \n\t"
        "ldrb       %w[r_a]       , [%[r_a], %w[bit], SXTW]     \n\t"
        "lsl        %w[low]       , %w[low]     , %w[tmp]       \n\t"
        "lsl        %w[range]     , %w[range]   , %w[tmp]       \n\t"
        "uxth       %w[r_c]       , %w[low]                     \n\t"
        "strb       %w[r_a]       , [%[state]]                  \n\t"
        "cbnz       %w[r_c]       , 2f                          \n\t"
        "ldr        %[r_c]        , [%[c], %[byte]]             \n\t"
        "ldr        %[r_a]        , [%[c], %[end]]              \n\t"
        "ldrh       %w[tmp]       , [%[r_c]]                    \n\t"
        "cmp        %[r_c]        , %[r_a]                      \n\t"
        "b.ge       1f                                          \n\t"
        "add        %[r_a]        , %[r_c]      , #2            \n\t"
        "str        %[r_a]        , [%[c], %[byte]]             \n\t"
        "1:                                                     \n\t"
        "sub        %w[r_c]       , %w[low]     , #1            \n\t"
        "eor        %w[r_c]       , %w[r_c]     , %w[low]       \n\t"
        "rev        %w[tmp]       , %w[tmp]                     \n\t"
        "lsr        %w[r_c]       , %w[r_c]     , #15           \n\t"
        "lsr        %w[tmp]       , %w[tmp]     , #15           \n\t"
        "ldrb       %w[r_c]       , [%[r_b], %w[r_c], SXTW]     \n\t"
        "mov        %w[r_b]       , #0xFFFF                     \n\t"
        "mov        %w[r_a]       , #7                          \n\t"
        "sub        %w[tmp]       , %w[tmp]     , %w[r_b]       \n\t"
        "sub        %w[r_c]       , %w[r_a]     , %w[r_c]       \n\t"
        "lsl        %w[tmp]       , %w[tmp]     , %w[r_c]       \n\t"
        "add        %w[low]       , %w[low]     , %w[tmp]       \n\t"
        "2:                                                     \n\t"
        :    [bit]"=&r"(bit),
             [low]"+&r"(c->low),
           [range]"+&r"(c->range),
             [r_a]"=&r"(reg_a),
             [r_b]"=&r"(reg_b),
             [r_c]"=&r"(reg_c),
             [tmp]"=&r"(tmp)
        :        [c]"r"(c),
             [state]"r"(state),
            [tables]"r"(ff_h264_cabac_tables),
              [byte]"i"(offsetof(CABACContext, bytestream)),
               [end]"i"(offsetof(CABACContext, bytestream_end)),
          [norm_off]"I"(H264_NORM_SHIFT_OFFSET),
           [lps_off]"I"(H264_LPS_RANGE_OFFSET),
          [mlps_off]"I"(H264_MLPS_STATE_OFFSET + 128)
        : "memory", "cc"
        );

    return bit & 1;
}

#endif /* HAVE_INLINE_ASM */

#endif /* AVCODEC_AARCH64_CABAC_H */