2 * Copyright (c) 2013 Seppo Tomperi
3 * Copyright (c) 2013 - 2014 Pierre-Edouard Lepere
6 * This file is part of ffmpeg.
8 * ffmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * ffmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with ffmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 #include "libavutil/cpu.h"
25 #include "libavutil/x86/asm.h"
26 #include "libavutil/x86/cpu.h"
27 #include "libavcodec/get_bits.h" /* required for hevcdsp.h GetBitContext */
28 #include "libavcodec/hevcdsp.h"
29 #include "libavcodec/x86/hevcdsp.h"
32 #define LFC_FUNC(DIR, DEPTH, OPT) \
33 void ff_hevc_ ## DIR ## _loop_filter_chroma_ ## DEPTH ## _ ## OPT(uint8_t *_pix, ptrdiff_t _stride, int *_tc, uint8_t *_no_p, uint8_t *_no_q);
35 #define LFL_FUNC(DIR, DEPTH, OPT) \
36 void ff_hevc_ ## DIR ## _loop_filter_luma_ ## DEPTH ## _ ## OPT(uint8_t *_pix, ptrdiff_t stride, int *_beta, int *_tc, \
37 uint8_t *_no_p, uint8_t *_no_q);
39 #define LFC_FUNCS(type, depth) \
40 LFC_FUNC(h, depth, sse2) \
41 LFC_FUNC(v, depth, sse2)
43 #define LFL_FUNCS(type, depth) \
44 LFL_FUNC(h, depth, ssse3) \
45 LFL_FUNC(v, depth, ssse3)
48 LFC_FUNCS(uint8_t, 10)
50 LFL_FUNCS(uint8_t, 10)
53 #define mc_rep_func(name, bitd, step, W, opt) \
54 void ff_hevc_put_hevc_##name##W##_##bitd##_##opt(int16_t *_dst, ptrdiff_t dststride, \
55 uint8_t *_src, ptrdiff_t _srcstride, int height, \
56 intptr_t mx, intptr_t my, int width) \
61 for (i = 0; i < W; i += step) { \
62 src = _src + (i * ((bitd + 7) / 8)); \
64 ff_hevc_put_hevc_##name##step##_##bitd##_##opt(dst, dststride, src, _srcstride, height, mx, my, width); \
67 #define mc_rep_uni_func(name, bitd, step, W, opt) \
68 void ff_hevc_put_hevc_uni_##name##W##_##bitd##_##opt(uint8_t *_dst, ptrdiff_t dststride, \
69 uint8_t *_src, ptrdiff_t _srcstride, int height, \
70 intptr_t mx, intptr_t my, int width) \
75 for (i = 0; i < W; i += step) { \
76 src = _src + (i * ((bitd + 7) / 8)); \
77 dst = _dst + (i * ((bitd + 7) / 8)); \
78 ff_hevc_put_hevc_uni_##name##step##_##bitd##_##opt(dst, dststride, src, _srcstride, \
79 height, mx, my, width); \
82 #define mc_rep_bi_func(name, bitd, step, W, opt) \
83 void ff_hevc_put_hevc_bi_##name##W##_##bitd##_##opt(uint8_t *_dst, ptrdiff_t dststride, uint8_t *_src, \
84 ptrdiff_t _srcstride, int16_t* _src2, ptrdiff_t _src2stride, \
85 int height, intptr_t mx, intptr_t my, int width) \
91 for (i = 0; i < W ; i += step) { \
92 src = _src + (i * ((bitd + 7) / 8)); \
93 dst = _dst + (i * ((bitd + 7) / 8)); \
95 ff_hevc_put_hevc_bi_##name##step##_##bitd##_##opt(dst, dststride, src, _srcstride, src2, \
96 _src2stride, height, mx, my, width); \
100 #define mc_rep_funcs(name, bitd, step, W, opt) \
101 mc_rep_func(name, bitd, step, W, opt); \
102 mc_rep_uni_func(name, bitd, step, W, opt); \
103 mc_rep_bi_func(name, bitd, step, W, opt)
106 #if ARCH_X86_64 && HAVE_SSE4_EXTERNAL
108 mc_rep_funcs(pel_pixels, 8, 16, 64, sse4);
109 mc_rep_funcs(pel_pixels, 8, 16, 48, sse4);
110 mc_rep_funcs(pel_pixels, 8, 16, 32, sse4);
111 mc_rep_funcs(pel_pixels, 8, 8, 24, sse4);
113 mc_rep_funcs(pel_pixels,10, 8, 64, sse4);
114 mc_rep_funcs(pel_pixels,10, 8, 48, sse4);
115 mc_rep_funcs(pel_pixels,10, 8, 32, sse4);
116 mc_rep_funcs(pel_pixels,10, 8, 24, sse4);
117 mc_rep_funcs(pel_pixels,10, 8, 16, sse4);
118 mc_rep_funcs(pel_pixels,10, 4, 12, sse4);
120 mc_rep_funcs(epel_h, 8, 16, 64, sse4);
121 mc_rep_funcs(epel_h, 8, 16, 48, sse4);
122 mc_rep_funcs(epel_h, 8, 16, 32, sse4);
123 mc_rep_funcs(epel_h, 8, 8, 24, sse4);
124 mc_rep_funcs(epel_h,10, 8, 64, sse4);
125 mc_rep_funcs(epel_h,10, 8, 48, sse4);
126 mc_rep_funcs(epel_h,10, 8, 32, sse4);
127 mc_rep_funcs(epel_h,10, 8, 24, sse4);
128 mc_rep_funcs(epel_h,10, 8, 16, sse4);
129 mc_rep_funcs(epel_h,10, 4, 12, sse4);
130 mc_rep_funcs(epel_v, 8, 16, 64, sse4);
131 mc_rep_funcs(epel_v, 8, 16, 48, sse4);
132 mc_rep_funcs(epel_v, 8, 16, 32, sse4);
133 mc_rep_funcs(epel_v, 8, 8, 24, sse4);
134 mc_rep_funcs(epel_v,10, 8, 64, sse4);
135 mc_rep_funcs(epel_v,10, 8, 48, sse4);
136 mc_rep_funcs(epel_v,10, 8, 32, sse4);
137 mc_rep_funcs(epel_v,10, 8, 24, sse4);
138 mc_rep_funcs(epel_v,10, 8, 16, sse4);
139 mc_rep_funcs(epel_v,10, 4, 12, sse4);
140 mc_rep_funcs(epel_hv, 8, 8, 64, sse4);
141 mc_rep_funcs(epel_hv, 8, 8, 48, sse4);
142 mc_rep_funcs(epel_hv, 8, 8, 32, sse4);
143 mc_rep_funcs(epel_hv, 8, 8, 24, sse4);
144 mc_rep_funcs(epel_hv, 8, 8, 16, sse4);
145 mc_rep_funcs(epel_hv, 8, 4, 12, sse4);
146 mc_rep_funcs(epel_hv,10, 8, 64, sse4);
147 mc_rep_funcs(epel_hv,10, 8, 48, sse4);
148 mc_rep_funcs(epel_hv,10, 8, 32, sse4);
149 mc_rep_funcs(epel_hv,10, 8, 24, sse4);
150 mc_rep_funcs(epel_hv,10, 8, 16, sse4);
151 mc_rep_funcs(epel_hv,10, 4, 12, sse4);
153 mc_rep_funcs(qpel_h, 8, 16, 64, sse4);
154 mc_rep_funcs(qpel_h, 8, 16, 48, sse4);
155 mc_rep_funcs(qpel_h, 8, 16, 32, sse4);
156 mc_rep_funcs(qpel_h, 8, 8, 24, sse4);
157 mc_rep_funcs(qpel_h,10, 8, 64, sse4);
158 mc_rep_funcs(qpel_h,10, 8, 48, sse4);
159 mc_rep_funcs(qpel_h,10, 8, 32, sse4);
160 mc_rep_funcs(qpel_h,10, 8, 24, sse4);
161 mc_rep_funcs(qpel_h,10, 8, 16, sse4);
162 mc_rep_funcs(qpel_h,10, 4, 12, sse4);
163 mc_rep_funcs(qpel_v, 8, 16, 64, sse4);
164 mc_rep_funcs(qpel_v, 8, 16, 48, sse4);
165 mc_rep_funcs(qpel_v, 8, 16, 32, sse4);
166 mc_rep_funcs(qpel_v, 8, 8, 24, sse4);
167 mc_rep_funcs(qpel_v,10, 8, 64, sse4);
168 mc_rep_funcs(qpel_v,10, 8, 48, sse4);
169 mc_rep_funcs(qpel_v,10, 8, 32, sse4);
170 mc_rep_funcs(qpel_v,10, 8, 24, sse4);
171 mc_rep_funcs(qpel_v,10, 8, 16, sse4);
172 mc_rep_funcs(qpel_v,10, 4, 12, sse4);
173 mc_rep_funcs(qpel_hv, 8, 8, 64, sse4);
174 mc_rep_funcs(qpel_hv, 8, 8, 48, sse4);
175 mc_rep_funcs(qpel_hv, 8, 8, 32, sse4);
176 mc_rep_funcs(qpel_hv, 8, 8, 24, sse4);
177 mc_rep_funcs(qpel_hv, 8, 8, 16, sse4);
178 mc_rep_funcs(qpel_hv, 8, 4, 12, sse4);
179 mc_rep_funcs(qpel_hv,10, 8, 64, sse4);
180 mc_rep_funcs(qpel_hv,10, 8, 48, sse4);
181 mc_rep_funcs(qpel_hv,10, 8, 32, sse4);
182 mc_rep_funcs(qpel_hv,10, 8, 24, sse4);
183 mc_rep_funcs(qpel_hv,10, 8, 16, sse4);
184 mc_rep_funcs(qpel_hv,10, 4, 12, sse4);
186 #define mc_rep_uni_w(bitd, step, W, opt) \
187 void ff_hevc_put_hevc_uni_w##W##_##bitd##_##opt(uint8_t *_dst, ptrdiff_t dststride, int16_t *_src, ptrdiff_t _srcstride,\
188 int height, int denom, int _wx, int _ox) \
193 for (i = 0; i < W; i += step) { \
195 dst= _dst + (i * ((bitd + 7) / 8)); \
196 ff_hevc_put_hevc_uni_w##step##_##bitd##_##opt(dst, dststride, src, _srcstride, \
197 height, denom, _wx, _ox); \
201 mc_rep_uni_w(8, 6, 12, sse4);
202 mc_rep_uni_w(8, 8, 16, sse4);
203 mc_rep_uni_w(8, 8, 24, sse4);
204 mc_rep_uni_w(8, 8, 32, sse4);
205 mc_rep_uni_w(8, 8, 48, sse4);
206 mc_rep_uni_w(8, 8, 64, sse4);
208 mc_rep_uni_w(10, 6, 12, sse4);
209 mc_rep_uni_w(10, 8, 16, sse4);
210 mc_rep_uni_w(10, 8, 24, sse4);
211 mc_rep_uni_w(10, 8, 32, sse4);
212 mc_rep_uni_w(10, 8, 48, sse4);
213 mc_rep_uni_w(10, 8, 64, sse4);
215 #define mc_rep_bi_w(bitd, step, W, opt) \
216 void ff_hevc_put_hevc_bi_w##W##_##bitd##_##opt(uint8_t *_dst, ptrdiff_t dststride, int16_t *_src, ptrdiff_t _srcstride, \
217 int16_t *_src2, ptrdiff_t _src2stride, int height, \
218 int denom, int _wx0, int _wx1, int _ox0, int _ox1) \
224 for (i = 0; i < W; i += step) { \
227 dst = _dst + (i * ((bitd + 7) / 8)); \
228 ff_hevc_put_hevc_bi_w##step##_##bitd##_##opt(dst, dststride, src, _srcstride, src2, _src2stride, \
229 height, denom, _wx0, _wx1, _ox0, _ox1); \
233 mc_rep_bi_w(8, 6, 12, sse4);
234 mc_rep_bi_w(8, 8, 16, sse4);
235 mc_rep_bi_w(8, 8, 24, sse4);
236 mc_rep_bi_w(8, 8, 32, sse4);
237 mc_rep_bi_w(8, 8, 48, sse4);
238 mc_rep_bi_w(8, 8, 64, sse4);
240 mc_rep_bi_w(10, 6, 12, sse4);
241 mc_rep_bi_w(10, 8, 16, sse4);
242 mc_rep_bi_w(10, 8, 24, sse4);
243 mc_rep_bi_w(10, 8, 32, sse4);
244 mc_rep_bi_w(10, 8, 48, sse4);
245 mc_rep_bi_w(10, 8, 64, sse4);
247 #define mc_uni_w_func(name, bitd, W, opt) \
248 void ff_hevc_put_hevc_uni_w_##name##W##_##bitd##_##opt(uint8_t *_dst, ptrdiff_t _dststride, \
249 uint8_t *_src, ptrdiff_t _srcstride, \
250 int height, int denom, \
252 intptr_t mx, intptr_t my, int width) \
254 LOCAL_ALIGNED_16(int16_t, temp, [71 * 64]); \
255 ff_hevc_put_hevc_##name##W##_##bitd##_##opt(temp, 64, _src, _srcstride, height, mx, my, width); \
256 ff_hevc_put_hevc_uni_w##W##_##bitd##_##opt(_dst, _dststride, temp, 64, height, denom, _wx, _ox);\
259 #define mc_uni_w_funcs(name, bitd, opt) \
260 mc_uni_w_func(name, bitd, 4, opt); \
261 mc_uni_w_func(name, bitd, 8, opt); \
262 mc_uni_w_func(name, bitd, 12, opt); \
263 mc_uni_w_func(name, bitd, 16, opt); \
264 mc_uni_w_func(name, bitd, 24, opt); \
265 mc_uni_w_func(name, bitd, 32, opt); \
266 mc_uni_w_func(name, bitd, 48, opt); \
267 mc_uni_w_func(name, bitd, 64, opt)
269 mc_uni_w_funcs(pel_pixels, 8, sse4);
270 mc_uni_w_func(pel_pixels, 8, 6, sse4);
271 mc_uni_w_funcs(epel_h, 8, sse4);
272 mc_uni_w_func(epel_h, 8, 6, sse4);
273 mc_uni_w_funcs(epel_v, 8, sse4);
274 mc_uni_w_func(epel_v, 8, 6, sse4);
275 mc_uni_w_funcs(epel_hv, 8, sse4);
276 mc_uni_w_func(epel_hv, 8, 6, sse4);
277 mc_uni_w_funcs(qpel_h, 8, sse4);
278 mc_uni_w_funcs(qpel_v, 8, sse4);
279 mc_uni_w_funcs(qpel_hv, 8, sse4);
281 mc_uni_w_funcs(pel_pixels, 10, sse4);
282 mc_uni_w_func(pel_pixels, 10, 6, sse4);
283 mc_uni_w_funcs(epel_h, 10, sse4);
284 mc_uni_w_func(epel_h, 10, 6, sse4);
285 mc_uni_w_funcs(epel_v, 10, sse4);
286 mc_uni_w_func(epel_v, 10, 6, sse4);
287 mc_uni_w_funcs(epel_hv, 10, sse4);
288 mc_uni_w_func(epel_hv, 10, 6, sse4);
289 mc_uni_w_funcs(qpel_h, 10, sse4);
290 mc_uni_w_funcs(qpel_v, 10, sse4);
291 mc_uni_w_funcs(qpel_hv, 10, sse4);
294 #define mc_bi_w_func(name, bitd, W, opt) \
295 void ff_hevc_put_hevc_bi_w_##name##W##_##bitd##_##opt(uint8_t *_dst, ptrdiff_t _dststride, \
296 uint8_t *_src, ptrdiff_t _srcstride, \
297 int16_t *_src2, ptrdiff_t _src2stride, \
298 int height, int denom, \
299 int _wx0, int _wx1, int _ox0, int _ox1, \
300 intptr_t mx, intptr_t my, int width) \
302 LOCAL_ALIGNED_16(int16_t, temp, [71 * 64]); \
303 ff_hevc_put_hevc_##name##W##_##bitd##_##opt(temp, 64, _src, _srcstride, height, mx, my, width); \
304 ff_hevc_put_hevc_bi_w##W##_##bitd##_##opt(_dst, _dststride, temp, 64, _src2, _src2stride, \
305 height, denom, _wx0, _wx1, _ox0, _ox1); \
308 #define mc_bi_w_funcs(name, bitd, opt) \
309 mc_bi_w_func(name, bitd, 4, opt); \
310 mc_bi_w_func(name, bitd, 8, opt); \
311 mc_bi_w_func(name, bitd, 12, opt); \
312 mc_bi_w_func(name, bitd, 16, opt); \
313 mc_bi_w_func(name, bitd, 24, opt); \
314 mc_bi_w_func(name, bitd, 32, opt); \
315 mc_bi_w_func(name, bitd, 48, opt); \
316 mc_bi_w_func(name, bitd, 64, opt)
318 mc_bi_w_funcs(pel_pixels, 8, sse4);
319 mc_bi_w_func(pel_pixels, 8, 6, sse4);
320 mc_bi_w_funcs(epel_h, 8, sse4);
321 mc_bi_w_func(epel_h, 8, 6, sse4);
322 mc_bi_w_funcs(epel_v, 8, sse4);
323 mc_bi_w_func(epel_v, 8, 6, sse4);
324 mc_bi_w_funcs(epel_hv, 8, sse4);
325 mc_bi_w_func(epel_hv, 8, 6, sse4);
326 mc_bi_w_funcs(qpel_h, 8, sse4);
327 mc_bi_w_funcs(qpel_v, 8, sse4);
328 mc_bi_w_funcs(qpel_hv, 8, sse4);
330 mc_bi_w_funcs(pel_pixels, 10, sse4);
331 mc_bi_w_func(pel_pixels, 10, 6, sse4);
332 mc_bi_w_funcs(epel_h, 10, sse4);
333 mc_bi_w_func(epel_h, 10, 6, sse4);
334 mc_bi_w_funcs(epel_v, 10, sse4);
335 mc_bi_w_func(epel_v, 10, 6, sse4);
336 mc_bi_w_funcs(epel_hv, 10, sse4);
337 mc_bi_w_func(epel_hv, 10, 6, sse4);
338 mc_bi_w_funcs(qpel_h, 10, sse4);
339 mc_bi_w_funcs(qpel_v, 10, sse4);
340 mc_bi_w_funcs(qpel_hv, 10, sse4);
342 #endif //ARCH_X86_64 && HAVE_SSE4_EXTERNAL
345 #define EPEL_LINKS(pointer, my, mx, fname, bitd, opt ) \
346 PEL_LINK(pointer, 1, my , mx , fname##4 , bitd, sse4 ); \
347 PEL_LINK(pointer, 2, my , mx , fname##6 , bitd, sse4 ); \
348 PEL_LINK(pointer, 3, my , mx , fname##8 , bitd, sse4 ); \
349 PEL_LINK(pointer, 4, my , mx , fname##12, bitd, sse4 ); \
350 PEL_LINK(pointer, 5, my , mx , fname##16, bitd, sse4 ); \
351 PEL_LINK(pointer, 6, my , mx , fname##24, bitd, sse4 ); \
352 PEL_LINK(pointer, 7, my , mx , fname##32, bitd, sse4 ); \
353 PEL_LINK(pointer, 8, my , mx , fname##48, bitd, sse4 ); \
354 PEL_LINK(pointer, 9, my , mx , fname##64, bitd, sse4 )
355 #define QPEL_LINKS(pointer, my, mx, fname, bitd, sse4) \
356 PEL_LINK(pointer, 1, my , mx , fname##4 , bitd, sse4 ); \
357 PEL_LINK(pointer, 3, my , mx , fname##8 , bitd, sse4 ); \
358 PEL_LINK(pointer, 4, my , mx , fname##12, bitd, sse4 ); \
359 PEL_LINK(pointer, 5, my , mx , fname##16, bitd, sse4 ); \
360 PEL_LINK(pointer, 6, my , mx , fname##24, bitd, sse4 ); \
361 PEL_LINK(pointer, 7, my , mx , fname##32, bitd, sse4 ); \
362 PEL_LINK(pointer, 8, my , mx , fname##48, bitd, sse4 ); \
363 PEL_LINK(pointer, 9, my , mx , fname##64, bitd, sse4 )
366 void ff_hevcdsp_init_x86(HEVCDSPContext *c, const int bit_depth)
368 int mm_flags = av_get_cpu_flags();
370 if (bit_depth == 8) {
371 if (EXTERNAL_SSE2(mm_flags)) {
372 c->hevc_v_loop_filter_chroma = ff_hevc_v_loop_filter_chroma_8_sse2;
373 c->hevc_h_loop_filter_chroma = ff_hevc_h_loop_filter_chroma_8_sse2;
375 if (EXTERNAL_SSSE3(mm_flags) && ARCH_X86_64) {
376 c->hevc_v_loop_filter_luma = ff_hevc_v_loop_filter_luma_8_ssse3;
377 c->hevc_h_loop_filter_luma = ff_hevc_h_loop_filter_luma_8_ssse3;
379 if (EXTERNAL_SSE4(mm_flags) && ARCH_X86_64) {
381 EPEL_LINKS(c->put_hevc_epel, 0, 0, pel_pixels, 8, sse4);
382 EPEL_LINKS(c->put_hevc_epel, 0, 1, epel_h, 8, sse4);
383 EPEL_LINKS(c->put_hevc_epel, 1, 0, epel_v, 8, sse4);
384 EPEL_LINKS(c->put_hevc_epel, 1, 1, epel_hv, 8, sse4);
386 QPEL_LINKS(c->put_hevc_qpel, 0, 0, pel_pixels, 8, sse4);
387 QPEL_LINKS(c->put_hevc_qpel, 0, 1, qpel_h, 8, sse4);
388 QPEL_LINKS(c->put_hevc_qpel, 1, 0, qpel_v, 8, sse4);
389 QPEL_LINKS(c->put_hevc_qpel, 1, 1, qpel_hv, 8, sse4);
392 } else if (bit_depth == 10) {
393 if (EXTERNAL_SSE2(mm_flags)) {
394 c->hevc_v_loop_filter_chroma = ff_hevc_v_loop_filter_chroma_10_sse2;
395 c->hevc_h_loop_filter_chroma = ff_hevc_h_loop_filter_chroma_10_sse2;
397 if (EXTERNAL_SSSE3(mm_flags) && ARCH_X86_64) {
398 c->hevc_v_loop_filter_luma = ff_hevc_v_loop_filter_luma_10_ssse3;
399 c->hevc_h_loop_filter_luma = ff_hevc_h_loop_filter_luma_10_ssse3;
401 if (EXTERNAL_SSE4(mm_flags) && ARCH_X86_64) {
403 EPEL_LINKS(c->put_hevc_epel, 0, 0, pel_pixels, 10, sse4);
404 EPEL_LINKS(c->put_hevc_epel, 0, 1, epel_h, 10, sse4);
405 EPEL_LINKS(c->put_hevc_epel, 1, 0, epel_v, 10, sse4);
406 EPEL_LINKS(c->put_hevc_epel, 1, 1, epel_hv, 10, sse4);
408 QPEL_LINKS(c->put_hevc_qpel, 0, 0, pel_pixels, 10, sse4);
409 QPEL_LINKS(c->put_hevc_qpel, 0, 1, qpel_h, 10, sse4);
410 QPEL_LINKS(c->put_hevc_qpel, 1, 0, qpel_v, 10, sse4);
411 QPEL_LINKS(c->put_hevc_qpel, 1, 1, qpel_hv, 10, sse4);