dcba42d3ed7973b61cdf6e78c5df6c81958941c4
[ffmpeg.git] / libavcodec / x86 / h264_i386.h
1 /*
2  * H.26L/H.264/AVC/JVT/14496-10/... encoder/decoder
3  * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 /**
23  * @file
24  * H.264 / AVC / MPEG4 part10 codec.
25  * non-MMX i386-specific optimizations for H.264
26  * @author Michael Niedermayer <michaelni@gmx.at>
27  */
28
29 #ifndef AVCODEC_X86_H264_I386_H
30 #define AVCODEC_X86_H264_I386_H
31
32 #include <stddef.h>
33
34 #include "libavcodec/cabac.h"
35 #include "cabac.h"
36
37 #if HAVE_INLINE_ASM
38
39 #if ARCH_X86_64
40 #define REG64 "r"
41 #else
42 #define REG64 "m"
43 #endif
44
45 //FIXME use some macros to avoid duplicating get_cabac (cannot be done yet
46 //as that would make optimization work hard)
47 #if HAVE_7REGS && !BROKEN_COMPILER
48 #define decode_significance decode_significance_x86
49 static int decode_significance_x86(CABACContext *c, int max_coeff,
50                                    uint8_t *significant_coeff_ctx_base,
51                                    int *index, x86_reg last_off){
52     void *end= significant_coeff_ctx_base + max_coeff - 1;
53     int minusstart= -(intptr_t)significant_coeff_ctx_base;
54     int minusindex= 4-(intptr_t)index;
55     int bit;
56     x86_reg coeff_count;
57
58 #ifdef BROKEN_RELOCATIONS
59     void *tables;
60
61     __asm__ volatile(
62         "lea   "MANGLE(ff_h264_cabac_tables)", %0      \n\t"
63         : "=&r"(tables)
64         : NAMED_CONSTRAINTS_ARRAY(ff_h264_cabac_tables)
65     );
66 #endif
67
68     __asm__ volatile(
69         "3:                                     \n\t"
70
71         BRANCHLESS_GET_CABAC("%4", "%q4", "(%1)", "%3", "%w3",
72                              "%5", "%q5", "%k0", "%b0",
73                              "%c11(%6)", "%c12(%6)",
74                              AV_STRINGIFY(H264_NORM_SHIFT_OFFSET),
75                              AV_STRINGIFY(H264_LPS_RANGE_OFFSET),
76                              AV_STRINGIFY(H264_MLPS_STATE_OFFSET),
77                              "%13")
78
79         "test $1, %4                            \n\t"
80         " jz 4f                                 \n\t"
81         "add  %10, %1                           \n\t"
82
83         BRANCHLESS_GET_CABAC("%4", "%q4", "(%1)", "%3", "%w3",
84                              "%5", "%q5", "%k0", "%b0",
85                              "%c11(%6)", "%c12(%6)",
86                              AV_STRINGIFY(H264_NORM_SHIFT_OFFSET),
87                              AV_STRINGIFY(H264_LPS_RANGE_OFFSET),
88                              AV_STRINGIFY(H264_MLPS_STATE_OFFSET),
89                              "%13")
90
91         "sub  %10, %1                           \n\t"
92         "mov  %2, %0                            \n\t"
93         "movl %7, %%ecx                         \n\t"
94         "add  %1, %%"REG_c"                     \n\t"
95         "movl %%ecx, (%0)                       \n\t"
96
97         "test $1, %4                            \n\t"
98         " jnz 5f                                \n\t"
99
100         "add"OPSIZE"  $4, %2                    \n\t"
101
102         "4:                                     \n\t"
103         "add  $1, %1                            \n\t"
104         "cmp  %8, %1                            \n\t"
105         " jb 3b                                 \n\t"
106         "mov  %2, %0                            \n\t"
107         "movl %7, %%ecx                         \n\t"
108         "add  %1, %%"REG_c"                     \n\t"
109         "movl %%ecx, (%0)                       \n\t"
110         "5:                                     \n\t"
111         "add  %9, %k0                           \n\t"
112         "shr $2, %k0                            \n\t"
113         : "=&q"(coeff_count), "+r"(significant_coeff_ctx_base), "+m"(index),
114           "+&r"(c->low), "=&r"(bit), "+&r"(c->range)
115         : "r"(c), "m"(minusstart), "m"(end), "m"(minusindex), "m"(last_off),
116           "i"(offsetof(CABACContext, bytestream)),
117           "i"(offsetof(CABACContext, bytestream_end))
118           TABLES_ARG
119         : "%"REG_c, "memory"
120     );
121     return coeff_count;
122 }
123
124 #define decode_significance_8x8 decode_significance_8x8_x86
125 static int decode_significance_8x8_x86(CABACContext *c,
126                                        uint8_t *significant_coeff_ctx_base,
127                                        int *index, uint8_t *last_coeff_ctx_base, const uint8_t *sig_off){
128     int minusindex= 4-(intptr_t)index;
129     int bit;
130     x86_reg coeff_count;
131     x86_reg last=0;
132     x86_reg state;
133
134 #ifdef BROKEN_RELOCATIONS
135     void *tables;
136
137     __asm__ volatile(
138         "lea    "MANGLE(ff_h264_cabac_tables)", %0      \n\t"
139         : "=&r"(tables)
140         : NAMED_CONSTRAINTS_ARRAY(ff_h264_cabac_tables)
141     );
142 #endif
143
144     __asm__ volatile(
145         "mov %1, %6                             \n\t"
146         "3:                                     \n\t"
147
148         "mov %10, %0                            \n\t"
149         "movzb (%0, %6), %6                     \n\t"
150         "add %9, %6                             \n\t"
151
152         BRANCHLESS_GET_CABAC("%4", "%q4", "(%6)", "%3", "%w3",
153                              "%5", "%q5", "%k0", "%b0",
154                              "%c12(%7)", "%c13(%7)",
155                              AV_STRINGIFY(H264_NORM_SHIFT_OFFSET),
156                              AV_STRINGIFY(H264_LPS_RANGE_OFFSET),
157                              AV_STRINGIFY(H264_MLPS_STATE_OFFSET),
158                              "%15")
159
160         "mov %1, %6                             \n\t"
161         "test $1, %4                            \n\t"
162         " jz 4f                                 \n\t"
163
164 #ifdef BROKEN_RELOCATIONS
165         "movzb %c14(%15, %q6), %6\n\t"
166 #else
167         "movzb "MANGLE(ff_h264_cabac_tables)"+%c14(%6), %6\n\t"
168 #endif
169         "add %11, %6                            \n\t"
170
171         BRANCHLESS_GET_CABAC("%4", "%q4", "(%6)", "%3", "%w3",
172                              "%5", "%q5", "%k0", "%b0",
173                              "%c12(%7)", "%c13(%7)",
174                              AV_STRINGIFY(H264_NORM_SHIFT_OFFSET),
175                              AV_STRINGIFY(H264_LPS_RANGE_OFFSET),
176                              AV_STRINGIFY(H264_MLPS_STATE_OFFSET),
177                              "%15")
178
179         "mov %2, %0                             \n\t"
180         "mov %1, %6                             \n\t"
181         "mov %6, (%0)                           \n\t"
182
183         "test $1, %4                            \n\t"
184         " jnz 5f                                \n\t"
185
186         "add"OPSIZE"  $4, %2                    \n\t"
187
188         "4:                                     \n\t"
189         "add $1, %6                             \n\t"
190         "mov %6, %1                             \n\t"
191         "cmp $63, %6                            \n\t"
192         " jb 3b                                 \n\t"
193         "mov %2, %0                             \n\t"
194         "mov %6, (%0)                           \n\t"
195         "5:                                     \n\t"
196         "addl %8, %k0                           \n\t"
197         "shr $2, %k0                            \n\t"
198         : "=&q"(coeff_count), "+"REG64(last), "+"REG64(index), "+&r"(c->low),
199           "=&r"(bit), "+&r"(c->range), "=&r"(state)
200         : "r"(c), "m"(minusindex), "m"(significant_coeff_ctx_base),
201           REG64(sig_off), REG64(last_coeff_ctx_base),
202           "i"(offsetof(CABACContext, bytestream)),
203           "i"(offsetof(CABACContext, bytestream_end)),
204           "i"(H264_LAST_COEFF_FLAG_OFFSET_8x8_OFFSET) TABLES_ARG
205         : "%"REG_c, "memory"
206     );
207     return coeff_count;
208 }
209 #endif /* HAVE_7REGS && BROKEN_COMPILER */
210
211 #endif /* HAVE_INLINE_ASM */
212 #endif /* AVCODEC_X86_H264_I386_H */