23 #include "libswscale/swscale.h"
25 #include "libavutil/attributes.h"
26 #include "libavutil/avassert.h"
27 #include "libavutil/intreadwrite.h"
30 #include "libavutil/cpu.h"
31 #include "libavutil/pixdesc.h"
44 0x0200020002000200LL,};
48 0x0004000400040004LL,};
62 DECLARE_ALIGNED(8,
const uint64_t, ff_bgr2YCoeff) = 0x000000210041000DULL;
63 DECLARE_ALIGNED(8,
const uint64_t, ff_bgr2UCoeff) = 0x0000FFEEFFDC0038ULL;
64 DECLARE_ALIGNED(8,
const uint64_t, ff_bgr2VCoeff) = 0x00000038FFD2FFF8ULL;
66 DECLARE_ALIGNED(8,
const uint64_t, ff_bgr2YCoeff) = 0x000020E540830C8BULL;
67 DECLARE_ALIGNED(8,
const uint64_t, ff_bgr2UCoeff) = 0x0000ED0FDAC23831ULL;
68 DECLARE_ALIGNED(8,
const uint64_t, ff_bgr2VCoeff) = 0x00003831D0E6F6EAULL;
70 DECLARE_ALIGNED(8,
const uint64_t, ff_bgr2YOffset) = 0x1010101010101010ULL;
71 DECLARE_ALIGNED(8,
const uint64_t, ff_bgr2UVOffset) = 0x8080808080808080ULL;
78 #define COMPILE_TEMPLATE_MMXEXT 0
79 #define RENAME(a) a ## _MMX
84 #if HAVE_MMXEXT_INLINE
86 #undef COMPILE_TEMPLATE_MMXEXT
87 #define COMPILE_TEMPLATE_MMXEXT 1
88 #define RENAME(a) a ## _MMXEXT
112 const int firstLumSrcY= vLumFilterPos[
dstY];
113 const int firstChrSrcY= vChrFilterPos[chrDstY];
121 if (dstY < dstH - 2) {
122 const int16_t **lumSrcPtr= (
const int16_t **)(
void*) lumPixBuf + lumBufIndex + firstLumSrcY - lastInLumBuf +
vLumBufSize;
123 const int16_t **chrUSrcPtr= (
const int16_t **)(
void*) chrUPixBuf + chrBufIndex + firstChrSrcY - lastInChrBuf +
vChrBufSize;
124 const int16_t **alpSrcPtr= (
CONFIG_SWSCALE_ALPHA &&
alpPixBuf) ? (
const int16_t **)(
void*) alpPixBuf + lumBufIndex + firstLumSrcY - lastInLumBuf + vLumBufSize :
NULL;
127 if (firstLumSrcY < 0 || firstLumSrcY + vLumFilterSize > c->
srcH) {
128 const int16_t **tmpY = (
const int16_t **) lumPixBuf + 2 * vLumBufSize;
129 int neg = -firstLumSrcY, i,
end =
FFMIN(c->
srcH - firstLumSrcY, vLumFilterSize);
130 for (i = 0; i < neg; i++)
131 tmpY[i] = lumSrcPtr[neg];
132 for ( ; i <
end; i++)
133 tmpY[i] = lumSrcPtr[i];
139 const int16_t **tmpA = (
const int16_t **) alpPixBuf + 2 * vLumBufSize;
140 for (i = 0; i < neg; i++)
141 tmpA[i] = alpSrcPtr[neg];
142 for ( ; i <
end; i++)
143 tmpA[i] = alpSrcPtr[i];
145 tmpA[i] = tmpA[i - 1];
149 if (firstChrSrcY < 0 || firstChrSrcY + vChrFilterSize > c->
chrSrcH) {
150 const int16_t **tmpU = (
const int16_t **) chrUPixBuf + 2 * vChrBufSize;
151 int neg = -firstChrSrcY, i, end =
FFMIN(c->
chrSrcH - firstChrSrcY, vChrFilterSize);
152 for (i = 0; i < neg; i++) {
153 tmpU[i] = chrUSrcPtr[neg];
155 for ( ; i <
end; i++) {
156 tmpU[i] = chrUSrcPtr[i];
159 tmpU[i] = tmpU[i - 1];
167 *(
const void**)&lumMmxFilter[s*i ]= lumSrcPtr[i ];
168 *(
const void**)&lumMmxFilter[s*i+
APCK_PTR2/4 ]= lumSrcPtr[i+(vLumFilterSize>1)];
170 lumMmxFilter[s*i+
APCK_COEF/4+1]= vLumFilter[dstY*vLumFilterSize + i ]
171 + (vLumFilterSize>1 ? vLumFilter[dstY*vLumFilterSize + i + 1]<<16 : 0);
173 *(
const void**)&alpMmxFilter[s*i ]= alpSrcPtr[i ];
174 *(
const void**)&alpMmxFilter[s*i+
APCK_PTR2/4 ]= alpSrcPtr[i+(vLumFilterSize>1)];
180 *(
const void**)&chrMmxFilter[s*i ]= chrUSrcPtr[i ];
181 *(
const void**)&chrMmxFilter[s*i+
APCK_PTR2/4 ]= chrUSrcPtr[i+(vChrFilterSize>1)];
183 chrMmxFilter[s*i+
APCK_COEF/4+1]= vChrFilter[chrDstY*vChrFilterSize + i ]
184 + (vChrFilterSize>1 ? vChrFilter[chrDstY*vChrFilterSize + i + 1]<<16 : 0);
188 *(
const void**)&lumMmxFilter[4*i+0]= lumSrcPtr[i];
191 ((uint16_t)vLumFilter[dstY*vLumFilterSize + i])*0x10001
U;
193 *(
const void**)&alpMmxFilter[4*i+0]= alpSrcPtr[i];
195 alpMmxFilter[4*i+3]= lumMmxFilter[4*i+2];
199 *(
const void**)&chrMmxFilter[4*i+0]= chrUSrcPtr[i];
202 ((uint16_t)vChrFilter[chrDstY*vChrFilterSize + i])*0x10001
U;
209 static void yuv2yuvX_sse3(
const int16_t *
filter,
int filterSize,
213 if(((
int)dest) & 15){
214 return yuv2yuvX_MMXEXT(filter, filterSize, src, dest, dstW, dither, offset);
217 __asm__
volatile(
"movq (%0), %%xmm3\n\t"
218 "movdqa %%xmm3, %%xmm4\n\t"
219 "psrlq $24, %%xmm3\n\t"
220 "psllq $40, %%xmm4\n\t"
221 "por %%xmm4, %%xmm3\n\t"
225 __asm__
volatile(
"movq (%0), %%xmm3\n\t"
231 "pxor %%xmm0, %%xmm0\n\t"
232 "punpcklbw %%xmm0, %%xmm3\n\t"
233 "movd %0, %%xmm1\n\t"
234 "punpcklwd %%xmm1, %%xmm1\n\t"
235 "punpckldq %%xmm1, %%xmm1\n\t"
236 "punpcklqdq %%xmm1, %%xmm1\n\t"
237 "psllw $3, %%xmm1\n\t"
238 "paddw %%xmm1, %%xmm3\n\t"
239 "psraw $4, %%xmm3\n\t"
243 "movdqa %%xmm3, %%xmm4\n\t"
244 "movdqa %%xmm3, %%xmm7\n\t"
246 "mov %0, %%"REG_d
" \n\t"\
247 "mov (%%"REG_d
"), %%"REG_S
" \n\t"\
250 "movddup 8(%%"REG_d
"), %%xmm0 \n\t" \
251 "movdqa (%%"REG_S
", %%"REG_c
", 2), %%xmm2 \n\t" \
252 "movdqa 16(%%"REG_S
", %%"REG_c
", 2), %%xmm5 \n\t" \
253 "add $16, %%"REG_d
" \n\t"\
254 "mov (%%"REG_d
"), %%"REG_S
" \n\t"\
255 "test %%"REG_S
", %%"REG_S
" \n\t"\
256 "pmulhw %%xmm0, %%xmm2 \n\t"\
257 "pmulhw %%xmm0, %%xmm5 \n\t"\
258 "paddw %%xmm2, %%xmm3 \n\t"\
259 "paddw %%xmm5, %%xmm4 \n\t"\
261 "psraw $3, %%xmm3 \n\t"\
262 "psraw $3, %%xmm4 \n\t"\
263 "packuswb %%xmm4, %%xmm3 \n\t"
264 "movntdq %%xmm3, (%1, %%"REG_c
")\n\t"
265 "add $16, %%"REG_c
" \n\t"\
266 "cmp %2, %%"REG_c
" \n\t"\
267 "movdqa %%xmm7, %%xmm3\n\t"
268 "movdqa %%xmm7, %%xmm4\n\t"
269 "mov %0, %%"REG_d
" \n\t"\
270 "mov (%%"REG_d
"), %%"REG_S
" \n\t"\
273 "r" (dest-offset),
"g" ((
x86_reg)(dstW+offset)),
"m" (offset)
274 :
XMM_CLOBBERS(
"%xmm0" ,
"%xmm1" ,
"%xmm2" ,
"%xmm3" ,
"%xmm4" ,
"%xmm5" ,
"%xmm7" ,)
275 "%"REG_d,
"%"REG_S,
"%"REG_c
282 #define SCALE_FUNC(filter_n, from_bpc, to_bpc, opt) \
283 extern void ff_hscale ## from_bpc ## to ## to_bpc ## _ ## filter_n ## _ ## opt( \
284 SwsContext *c, int16_t *data, \
285 int dstW, const uint8_t *src, \
286 const int16_t *filter, \
287 const int32_t *filterPos, int filterSize)
289 #define SCALE_FUNCS(filter_n, opt) \
290 SCALE_FUNC(filter_n, 8, 15, opt); \
291 SCALE_FUNC(filter_n, 9, 15, opt); \
292 SCALE_FUNC(filter_n, 10, 15, opt); \
293 SCALE_FUNC(filter_n, 12, 15, opt); \
294 SCALE_FUNC(filter_n, 14, 15, opt); \
295 SCALE_FUNC(filter_n, 16, 15, opt); \
296 SCALE_FUNC(filter_n, 8, 19, opt); \
297 SCALE_FUNC(filter_n, 9, 19, opt); \
298 SCALE_FUNC(filter_n, 10, 19, opt); \
299 SCALE_FUNC(filter_n, 12, 19, opt); \
300 SCALE_FUNC(filter_n, 14, 19, opt); \
301 SCALE_FUNC(filter_n, 16, 19, opt)
303 #define SCALE_FUNCS_MMX(opt) \
304 SCALE_FUNCS(4, opt); \
305 SCALE_FUNCS(8, opt); \
308 #define SCALE_FUNCS_SSE(opt) \
309 SCALE_FUNCS(4, opt); \
310 SCALE_FUNCS(8, opt); \
311 SCALE_FUNCS(X4, opt); \
321 #define VSCALEX_FUNC(size, opt) \
322 extern void ff_yuv2planeX_ ## size ## _ ## opt(const int16_t *filter, int filterSize, \
323 const int16_t **src, uint8_t *dest, int dstW, \
324 const uint8_t *dither, int offset)
325 #define VSCALEX_FUNCS(opt) \
326 VSCALEX_FUNC(8, opt); \
327 VSCALEX_FUNC(9, opt); \
328 VSCALEX_FUNC(10, opt)
338 #define VSCALE_FUNC(size, opt) \
339 extern void ff_yuv2plane1_ ## size ## _ ## opt(const int16_t *src, uint8_t *dst, int dstW, \
340 const uint8_t *dither, int offset)
341 #define VSCALE_FUNCS(opt1, opt2) \
342 VSCALE_FUNC(8, opt1); \
343 VSCALE_FUNC(9, opt2); \
344 VSCALE_FUNC(10, opt2); \
345 VSCALE_FUNC(16, opt1)
354 #define INPUT_Y_FUNC(fmt, opt) \
355 extern void ff_ ## fmt ## ToY_ ## opt(uint8_t *dst, const uint8_t *src, \
356 const uint8_t *unused1, const uint8_t *unused2, \
357 int w, uint32_t *unused)
358 #define INPUT_UV_FUNC(fmt, opt) \
359 extern void ff_ ## fmt ## ToUV_ ## opt(uint8_t *dstU, uint8_t *dstV, \
360 const uint8_t *unused0, \
361 const uint8_t *src1, \
362 const uint8_t *src2, \
363 int w, uint32_t *unused)
364 #define INPUT_FUNC(fmt, opt) \
365 INPUT_Y_FUNC(fmt, opt); \
366 INPUT_UV_FUNC(fmt, opt)
367 #define INPUT_FUNCS(opt) \
368 INPUT_FUNC(uyvy, opt); \
369 INPUT_FUNC(yuyv, opt); \
370 INPUT_UV_FUNC(nv12, opt); \
371 INPUT_UV_FUNC(nv21, opt); \
372 INPUT_FUNC(rgba, opt); \
373 INPUT_FUNC(bgra, opt); \
374 INPUT_FUNC(argb, opt); \
375 INPUT_FUNC(abgr, opt); \
376 INPUT_FUNC(rgb24, opt); \
377 INPUT_FUNC(bgr24, opt)
392 sws_init_swScale_MMX(c);
393 #if HAVE_MMXEXT_INLINE
395 sws_init_swScale_MMXEXT(c);
403 #define ASSIGN_SCALE_FUNC2(hscalefn, filtersize, opt1, opt2) do { \
404 if (c->srcBpc == 8) { \
405 hscalefn = c->dstBpc <= 14 ? ff_hscale8to15_ ## filtersize ## _ ## opt2 : \
406 ff_hscale8to19_ ## filtersize ## _ ## opt1; \
407 } else if (c->srcBpc == 9) { \
408 hscalefn = c->dstBpc <= 14 ? ff_hscale9to15_ ## filtersize ## _ ## opt2 : \
409 ff_hscale9to19_ ## filtersize ## _ ## opt1; \
410 } else if (c->srcBpc == 10) { \
411 hscalefn = c->dstBpc <= 14 ? ff_hscale10to15_ ## filtersize ## _ ## opt2 : \
412 ff_hscale10to19_ ## filtersize ## _ ## opt1; \
413 } else if (c->srcBpc == 12) { \
414 hscalefn = c->dstBpc <= 14 ? ff_hscale12to15_ ## filtersize ## _ ## opt2 : \
415 ff_hscale12to19_ ## filtersize ## _ ## opt1; \
416 } else if (c->srcBpc == 14 || ((c->srcFormat==AV_PIX_FMT_PAL8||isAnyRGB(c->srcFormat)) && av_pix_fmt_desc_get(c->srcFormat)->comp[0].depth_minus1<15)) { \
417 hscalefn = c->dstBpc <= 14 ? ff_hscale14to15_ ## filtersize ## _ ## opt2 : \
418 ff_hscale14to19_ ## filtersize ## _ ## opt1; \
420 av_assert0(c->srcBpc == 16);\
421 hscalefn = c->dstBpc <= 14 ? ff_hscale16to15_ ## filtersize ## _ ## opt2 : \
422 ff_hscale16to19_ ## filtersize ## _ ## opt1; \
425 #define ASSIGN_MMX_SCALE_FUNC(hscalefn, filtersize, opt1, opt2) \
426 switch (filtersize) { \
427 case 4: ASSIGN_SCALE_FUNC2(hscalefn, 4, opt1, opt2); break; \
428 case 8: ASSIGN_SCALE_FUNC2(hscalefn, 8, opt1, opt2); break; \
429 default: ASSIGN_SCALE_FUNC2(hscalefn, X, opt1, opt2); break; \
431 #define ASSIGN_VSCALEX_FUNC(vscalefn, opt, do_16_case, condition_8bit) \
433 case 16: do_16_case; break; \
434 case 10: if (!isBE(c->dstFormat)) vscalefn = ff_yuv2planeX_10_ ## opt; break; \
435 case 9: if (!isBE(c->dstFormat)) vscalefn = ff_yuv2planeX_9_ ## opt; break; \
436 default: if (condition_8bit) break; \
438 #define ASSIGN_VSCALE_FUNC(vscalefn, opt1, opt2, opt2chk) \
440 case 16: if (!isBE(c->dstFormat)) vscalefn = ff_yuv2plane1_16_ ## opt1; break; \
441 case 10: if (!isBE(c->dstFormat) && opt2chk) vscalefn = ff_yuv2plane1_10_ ## opt2; break; \
442 case 9: if (!isBE(c->dstFormat) && opt2chk) vscalefn = ff_yuv2plane1_9_ ## opt2; break; \
443 case 8: vscalefn = ff_yuv2plane1_8_ ## opt1; break; \
444 default: av_assert0(c->dstBpc>8); \
446 #define case_rgb(x, X, opt) \
447 case AV_PIX_FMT_ ## X: \
448 c->lumToYV12 = ff_ ## x ## ToY_ ## opt; \
449 if (!c->chrSrcHSubSample) \
450 c->chrToYV12 = ff_ ## x ## ToUV_ ## opt; \
492 #define ASSIGN_SSE_SCALE_FUNC(hscalefn, filtersize, opt1, opt2) \
493 switch (filtersize) { \
494 case 4: ASSIGN_SCALE_FUNC2(hscalefn, 4, opt1, opt2); break; \
495 case 8: ASSIGN_SCALE_FUNC2(hscalefn, 8, opt1, opt2); break; \
496 default: if (filtersize & 4) ASSIGN_SCALE_FUNC2(hscalefn, X4, opt1, opt2); \
497 else ASSIGN_SCALE_FUNC2(hscalefn, X8, opt1, opt2); \