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 //FIXME use some macros to avoid duplicating get_cabac (cannot be done yet
40 //as that would make optimization work hard)
41 #if HAVE_7REGS && !BROKEN_COMPILER
42 #define decode_significance decode_significance_x86
decode_significance_x86(CABACContext * c,int max_coeff,uint8_t * significant_coeff_ctx_base,int * index,x86_reg last_off)43 static int decode_significance_x86(CABACContext *c, int max_coeff,
44                                    uint8_t *significant_coeff_ctx_base,
45                                    int *index, x86_reg last_off){
46     void *end= significant_coeff_ctx_base + max_coeff - 1;
47     int minusstart= -(intptr_t)significant_coeff_ctx_base;
48     int minusindex= 4-(intptr_t)index;
49     int bit;
50     x86_reg coeff_count;
51 
52 #ifdef BROKEN_RELOCATIONS
53     void *tables;
54 
55     __asm__ volatile(
56         "lea   "MANGLE(ff_h264_cabac_tables)", %0      \n\t"
57         : "=&r"(tables)
58         : NAMED_CONSTRAINTS_ARRAY(ff_h264_cabac_tables)
59     );
60 #endif
61 
62     __asm__ volatile(
63         "3:                                     \n\t"
64 
65         BRANCHLESS_GET_CABAC("%4", "%q4", "(%1)", "%3", "%w3",
66                              "%5", "%q5", "%k0", "%b0",
67                              "%c11(%6)", "%c12(%6)",
68                              AV_STRINGIFY(H264_NORM_SHIFT_OFFSET),
69                              AV_STRINGIFY(H264_LPS_RANGE_OFFSET),
70                              AV_STRINGIFY(H264_MLPS_STATE_OFFSET),
71                              "%13")
72 
73         "test $1, %4                            \n\t"
74         " jz 4f                                 \n\t"
75         "add  %10, %1                           \n\t"
76 
77         BRANCHLESS_GET_CABAC("%4", "%q4", "(%1)", "%3", "%w3",
78                              "%5", "%q5", "%k0", "%b0",
79                              "%c11(%6)", "%c12(%6)",
80                              AV_STRINGIFY(H264_NORM_SHIFT_OFFSET),
81                              AV_STRINGIFY(H264_LPS_RANGE_OFFSET),
82                              AV_STRINGIFY(H264_MLPS_STATE_OFFSET),
83                              "%13")
84 
85         "sub  %10, %1                           \n\t"
86         "mov  %2, %0                            \n\t"
87         "movl %7, %%ecx                         \n\t"
88         "add  %1, %%"REG_c"                     \n\t"
89         "movl %%ecx, (%0)                       \n\t"
90 
91         "test $1, %4                            \n\t"
92         " jnz 5f                                \n\t"
93 
94         "add"OPSIZE"  $4, %2                    \n\t"
95 
96         "4:                                     \n\t"
97         "add  $1, %1                            \n\t"
98         "cmp  %8, %1                            \n\t"
99         " jb 3b                                 \n\t"
100         "mov  %2, %0                            \n\t"
101         "movl %7, %%ecx                         \n\t"
102         "add  %1, %%"REG_c"                     \n\t"
103         "movl %%ecx, (%0)                       \n\t"
104         "5:                                     \n\t"
105         "add  %9, %k0                           \n\t"
106         "shr $2, %k0                            \n\t"
107         : "=&q"(coeff_count), "+r"(significant_coeff_ctx_base), "+m"(index),
108           "+&r"(c->low), "=&r"(bit), "+&r"(c->range)
109         : "r"(c), "m"(minusstart), "m"(end), "m"(minusindex), "m"(last_off),
110           "i"(offsetof(CABACContext, bytestream)),
111           "i"(offsetof(CABACContext, bytestream_end))
112           TABLES_ARG
113         : "%"REG_c, "memory"
114     );
115     return coeff_count;
116 }
117 
118 #define decode_significance_8x8 decode_significance_8x8_x86
decode_significance_8x8_x86(CABACContext * c,uint8_t * significant_coeff_ctx_base,int * index,uint8_t * last_coeff_ctx_base,const uint8_t * sig_off)119 static int decode_significance_8x8_x86(CABACContext *c,
120                                        uint8_t *significant_coeff_ctx_base,
121                                        int *index, uint8_t *last_coeff_ctx_base, const uint8_t *sig_off){
122     int minusindex= 4-(intptr_t)index;
123     int bit;
124     x86_reg coeff_count;
125     x86_reg last=0;
126     x86_reg state;
127 
128 #ifdef BROKEN_RELOCATIONS
129     void *tables;
130 
131     __asm__ volatile(
132         "lea    "MANGLE(ff_h264_cabac_tables)", %0      \n\t"
133         : "=&r"(tables)
134         : NAMED_CONSTRAINTS_ARRAY(ff_h264_cabac_tables)
135     );
136 #endif
137 
138     __asm__ volatile(
139         "mov %1, %6                             \n\t"
140         "3:                                     \n\t"
141 
142         "mov %10, %0                            \n\t"
143         "movzbl (%0, %6), %k6                   \n\t"
144         "add %9, %6                             \n\t"
145 
146         BRANCHLESS_GET_CABAC("%4", "%q4", "(%6)", "%3", "%w3",
147                              "%5", "%q5", "%k0", "%b0",
148                              "%c12(%7)", "%c13(%7)",
149                              AV_STRINGIFY(H264_NORM_SHIFT_OFFSET),
150                              AV_STRINGIFY(H264_LPS_RANGE_OFFSET),
151                              AV_STRINGIFY(H264_MLPS_STATE_OFFSET),
152                              "%15")
153 
154         "mov %1, %k6                            \n\t"
155         "test $1, %4                            \n\t"
156         " jz 4f                                 \n\t"
157 
158 #ifdef BROKEN_RELOCATIONS
159         "movzbl %c14(%15, %q6), %k6\n\t"
160 #else
161         "movzbl "MANGLE(ff_h264_cabac_tables)"+%c14(%k6), %k6\n\t"
162 #endif
163         "add %11, %6                            \n\t"
164 
165         BRANCHLESS_GET_CABAC("%4", "%q4", "(%6)", "%3", "%w3",
166                              "%5", "%q5", "%k0", "%b0",
167                              "%c12(%7)", "%c13(%7)",
168                              AV_STRINGIFY(H264_NORM_SHIFT_OFFSET),
169                              AV_STRINGIFY(H264_LPS_RANGE_OFFSET),
170                              AV_STRINGIFY(H264_MLPS_STATE_OFFSET),
171                              "%15")
172 
173         "mov %2, %0                             \n\t"
174         "mov %1, %k6                            \n\t"
175         "movl %k6, (%0)                         \n\t"
176 
177         "test $1, %4                            \n\t"
178         " jnz 5f                                \n\t"
179 
180         "add"OPSIZE"  $4, %2                    \n\t"
181 
182         "4:                                     \n\t"
183         "addl $1, %k6                           \n\t"
184         "mov %k6, %1                            \n\t"
185         "cmpl $63, %k6                          \n\t"
186         " jb 3b                                 \n\t"
187         "mov %2, %0                             \n\t"
188         "movl %k6, (%0)                         \n\t"
189         "5:                                     \n\t"
190         "addl %8, %k0                           \n\t"
191         "shr $2, %k0                            \n\t"
192         : "=&q"(coeff_count), "+m"(last), "+m"(index), "+&r"(c->low),
193           "=&r"(bit), "+&r"(c->range), "=&r"(state)
194         : "r"(c), "m"(minusindex), "m"(significant_coeff_ctx_base),
195           "m"(sig_off), "m"(last_coeff_ctx_base),
196           "i"(offsetof(CABACContext, bytestream)),
197           "i"(offsetof(CABACContext, bytestream_end)),
198           "i"(H264_LAST_COEFF_FLAG_OFFSET_8x8_OFFSET) TABLES_ARG
199         : "%"REG_c, "memory"
200     );
201     return coeff_count;
202 }
203 #endif /* HAVE_7REGS && BROKEN_COMPILER */
204 
205 #endif /* HAVE_INLINE_ASM */
206 #endif /* AVCODEC_X86_H264_I386_H */
207