cabac: add overread protection to BRANCHLESS_GET_CABAC().
[ffmpeg.git] / libavcodec / x86 / cabac.h
1 /*
2  * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
3  *
4  * This file is part of Libav.
5  *
6  * Libav is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * Libav is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with Libav; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20
21 #ifndef AVCODEC_X86_CABAC_H
22 #define AVCODEC_X86_CABAC_H
23
24 #include "libavcodec/cabac.h"
25 #include "libavutil/attributes.h"
26 #include "libavutil/x86_cpu.h"
27 #include "config.h"
28
29 #if HAVE_FAST_CMOV
30 #define BRANCHLESS_GET_CABAC_UPDATE(ret, statep, low, range, tmp)\
31         "mov    "tmp"       , %%ecx     \n\t"\
32         "shl    $17         , "tmp"     \n\t"\
33         "cmp    "low"       , "tmp"     \n\t"\
34         "cmova  %%ecx       , "range"   \n\t"\
35         "sbb    %%ecx       , %%ecx     \n\t"\
36         "and    %%ecx       , "tmp"     \n\t"\
37         "xor    %%ecx       , "ret"     \n\t"\
38         "sub    "tmp"       , "low"     \n\t"
39 #else /* HAVE_FAST_CMOV */
40 #define BRANCHLESS_GET_CABAC_UPDATE(ret, statep, low, range, tmp)\
41         "mov    "tmp"       , %%ecx     \n\t"\
42         "shl    $17         , "tmp"     \n\t"\
43         "sub    "low"       , "tmp"     \n\t"\
44         "sar    $31         , "tmp"     \n\t" /*lps_mask*/\
45         "sub    %%ecx       , "range"   \n\t" /*RangeLPS - range*/\
46         "and    "tmp"       , "range"   \n\t" /*(RangeLPS - range)&lps_mask*/\
47         "add    %%ecx       , "range"   \n\t" /*new range*/\
48         "shl    $17         , %%ecx     \n\t"\
49         "and    "tmp"       , %%ecx     \n\t"\
50         "sub    %%ecx       , "low"     \n\t"\
51         "xor    "tmp"       , "ret"     \n\t"
52 #endif /* HAVE_FAST_CMOV */
53
54 #define BRANCHLESS_GET_CABAC(ret, statep, low, lowword, range, tmp, tmpbyte, byte, end) \
55         "movzbl "statep"    , "ret"                                     \n\t"\
56         "mov    "range"     , "tmp"                                     \n\t"\
57         "and    $0xC0       , "range"                                   \n\t"\
58         "movzbl "MANGLE(ff_h264_lps_range)"("ret", "range", 2), "range" \n\t"\
59         "sub    "range"     , "tmp"                                     \n\t"\
60         BRANCHLESS_GET_CABAC_UPDATE(ret, statep, low, range, tmp)   \
61         "movzbl " MANGLE(ff_h264_norm_shift) "("range"), %%ecx          \n\t"\
62         "shl    %%cl        , "range"                                   \n\t"\
63         "movzbl "MANGLE(ff_h264_mlps_state)"+128("ret"), "tmp"          \n\t"\
64         "shl    %%cl        , "low"                                     \n\t"\
65         "mov    "tmpbyte"   , "statep"                                  \n\t"\
66         "test   "lowword"   , "lowword"                                 \n\t"\
67         " jnz   2f                                                      \n\t"\
68         "mov    "byte"      , %%"REG_c"                                 \n\t"\
69         "cmp    "end"       , %%"REG_c"                                 \n\t"\
70         "jge    1f                                                      \n\t"\
71         "add"OPSIZE" $2     , "byte"                                    \n\t"\
72         "1:                                                             \n\t"\
73         "movzwl (%%"REG_c")     , "tmp"                                 \n\t"\
74         "lea    -1("low")   , %%ecx                                     \n\t"\
75         "xor    "low"       , %%ecx                                     \n\t"\
76         "shr    $15         , %%ecx                                     \n\t"\
77         "bswap  "tmp"                                                   \n\t"\
78         "shr    $15         , "tmp"                                     \n\t"\
79         "movzbl " MANGLE(ff_h264_norm_shift) "(%%ecx), %%ecx            \n\t"\
80         "sub    $0xFFFF     , "tmp"                                     \n\t"\
81         "neg    %%ecx                                                   \n\t"\
82         "add    $7          , %%ecx                                     \n\t"\
83         "shl    %%cl        , "tmp"                                     \n\t"\
84         "add    "tmp"       , "low"                                     \n\t"\
85         "2:                                                             \n\t"
86
87 #if HAVE_7REGS && !defined(BROKEN_RELOCATIONS)
88 #define get_cabac_inline get_cabac_inline_x86
89 static av_always_inline int get_cabac_inline_x86(CABACContext *c,
90                                                  uint8_t *const state)
91 {
92     int bit, tmp;
93
94     __asm__ volatile(
95         BRANCHLESS_GET_CABAC("%0", "(%4)", "%1", "%w1",
96                              "%2", "%3", "%b3",
97                              "%a6(%5)", "%a7(%5)")
98         : "=&r"(bit), "+&r"(c->low), "+&r"(c->range), "=&q"(tmp)
99         : "r"(state), "r"(c),
100           "i"(offsetof(CABACContext, bytestream)),
101           "i"(offsetof(CABACContext, bytestream_end))
102         : "%"REG_c, "memory"
103     );
104     return bit & 1;
105 }
106 #endif /* HAVE_7REGS && !defined(BROKEN_RELOCATIONS) */
107
108 #define get_cabac_bypass_sign get_cabac_bypass_sign_x86
109 static av_always_inline int get_cabac_bypass_sign_x86(CABACContext *c, int val)
110 {
111     x86_reg tmp;
112     __asm__ volatile(
113         "movl        %a6(%2), %k1       \n\t"
114         "movl        %a3(%2), %%eax     \n\t"
115         "shl             $17, %k1       \n\t"
116         "add           %%eax, %%eax     \n\t"
117         "sub             %k1, %%eax     \n\t"
118         "cltd                           \n\t"
119         "and           %%edx, %k1       \n\t"
120         "add             %k1, %%eax     \n\t"
121         "xor           %%edx, %%ecx     \n\t"
122         "sub           %%edx, %%ecx     \n\t"
123         "test           %%ax, %%ax      \n\t"
124         "jnz              1f            \n\t"
125         "mov         %a4(%2), %1        \n\t"
126         "subl        $0xFFFF, %%eax     \n\t"
127         "movzwl         (%1), %%edx     \n\t"
128         "bswap         %%edx            \n\t"
129         "shrl            $15, %%edx     \n\t"
130         "addl          %%edx, %%eax     \n\t"
131         "cmp         %a5(%2), %1        \n\t"
132         "jge              1f            \n\t"
133         "add"OPSIZE"      $2, %a4(%2)   \n\t"
134         "1:                             \n\t"
135         "movl          %%eax, %a3(%2)   \n\t"
136
137         : "+c"(val), "=&r"(tmp)
138         : "r"(c),
139           "i"(offsetof(CABACContext, low)),
140           "i"(offsetof(CABACContext, bytestream)),
141           "i"(offsetof(CABACContext, bytestream_end)),
142           "i"(offsetof(CABACContext, range))
143         : "%eax", "%edx", "memory"
144     );
145     return val;
146 }
147
148 #endif /* AVCODEC_X86_CABAC_H */