swscale: Fix stack alignment for SSE
[libav.git] / libswscale / swscale_unscaled.c
1 /*
2 * Copyright (C) 2001-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 #include <inttypes.h>
22 #include <string.h>
23 #include <math.h>
24 #include <stdio.h>
25 #include "config.h"
26 #include <assert.h>
27 #include "swscale.h"
28 #include "swscale_internal.h"
29 #include "rgb2rgb.h"
30 #include "libavutil/intreadwrite.h"
31 #include "libavutil/cpu.h"
32 #include "libavutil/avutil.h"
33 #include "libavutil/mathematics.h"
34 #include "libavutil/bswap.h"
35 #include "libavutil/pixdesc.h"
36
37 DECLARE_ALIGNED(8, const uint8_t, dither_8x8_1)[8][8] = {
38 { 0, 1, 0, 1, 0, 1, 0, 1,},
39 { 1, 0, 1, 0, 1, 0, 1, 0,},
40 { 0, 1, 0, 1, 0, 1, 0, 1,},
41 { 1, 0, 1, 0, 1, 0, 1, 0,},
42 { 0, 1, 0, 1, 0, 1, 0, 1,},
43 { 1, 0, 1, 0, 1, 0, 1, 0,},
44 { 0, 1, 0, 1, 0, 1, 0, 1,},
45 { 1, 0, 1, 0, 1, 0, 1, 0,},
46 };
47 DECLARE_ALIGNED(8, const uint8_t, dither_8x8_3)[8][8] = {
48 { 1, 2, 1, 2, 1, 2, 1, 2,},
49 { 3, 0, 3, 0, 3, 0, 3, 0,},
50 { 1, 2, 1, 2, 1, 2, 1, 2,},
51 { 3, 0, 3, 0, 3, 0, 3, 0,},
52 { 1, 2, 1, 2, 1, 2, 1, 2,},
53 { 3, 0, 3, 0, 3, 0, 3, 0,},
54 { 1, 2, 1, 2, 1, 2, 1, 2,},
55 { 3, 0, 3, 0, 3, 0, 3, 0,},
56 };
57 DECLARE_ALIGNED(8, const uint8_t, dither_8x8_64)[8][8] = {
58 { 18, 34, 30, 46, 17, 33, 29, 45,},
59 { 50, 2, 62, 14, 49, 1, 61, 13,},
60 { 26, 42, 22, 38, 25, 41, 21, 37,},
61 { 58, 10, 54, 6, 57, 9, 53, 5,},
62 { 16, 32, 28, 44, 19, 35, 31, 47,},
63 { 48, 0, 60, 12, 51, 3, 63, 15,},
64 { 24, 40, 20, 36, 27, 43, 23, 39,},
65 { 56, 8, 52, 4, 59, 11, 55, 7,},
66 };
67 extern const uint8_t dither_8x8_128[8][8];
68 DECLARE_ALIGNED(8, const uint8_t, dither_8x8_256)[8][8] = {
69 { 72, 136, 120, 184, 68, 132, 116, 180,},
70 { 200, 8, 248, 56, 196, 4, 244, 52,},
71 { 104, 168, 88, 152, 100, 164, 84, 148,},
72 { 232, 40, 216, 24, 228, 36, 212, 20,},
73 { 64, 128, 102, 176, 76, 140, 124, 188,},
74 { 192, 0, 240, 48, 204, 12, 252, 60,},
75 { 96, 160, 80, 144, 108, 172, 92, 156,},
76 { 224, 32, 208, 16, 236, 44, 220, 28,},
77 };
78
79 #define RGB2YUV_SHIFT 15
80 #define BY ( (int)(0.114*219/255*(1<<RGB2YUV_SHIFT)+0.5))
81 #define BV (-(int)(0.081*224/255*(1<<RGB2YUV_SHIFT)+0.5))
82 #define BU ( (int)(0.500*224/255*(1<<RGB2YUV_SHIFT)+0.5))
83 #define GY ( (int)(0.587*219/255*(1<<RGB2YUV_SHIFT)+0.5))
84 #define GV (-(int)(0.419*224/255*(1<<RGB2YUV_SHIFT)+0.5))
85 #define GU (-(int)(0.331*224/255*(1<<RGB2YUV_SHIFT)+0.5))
86 #define RY ( (int)(0.299*219/255*(1<<RGB2YUV_SHIFT)+0.5))
87 #define RV ( (int)(0.500*224/255*(1<<RGB2YUV_SHIFT)+0.5))
88 #define RU (-(int)(0.169*224/255*(1<<RGB2YUV_SHIFT)+0.5))
89
90 static void fillPlane(uint8_t* plane, int stride, int width, int height, int y, uint8_t val)
91 {
92 int i;
93 uint8_t *ptr = plane + stride*y;
94 for (i=0; i<height; i++) {
95 memset(ptr, val, width);
96 ptr += stride;
97 }
98 }
99
100 static void copyPlane(const uint8_t *src, int srcStride,
101 int srcSliceY, int srcSliceH, int width,
102 uint8_t *dst, int dstStride)
103 {
104 dst += dstStride * srcSliceY;
105 if (dstStride == srcStride && srcStride > 0) {
106 memcpy(dst, src, srcSliceH * dstStride);
107 } else {
108 int i;
109 for (i=0; i<srcSliceH; i++) {
110 memcpy(dst, src, width);
111 src += srcStride;
112 dst += dstStride;
113 }
114 }
115 }
116
117 static int planarToNv12Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
118 int srcSliceH, uint8_t* dstParam[], int dstStride[])
119 {
120 uint8_t *dst = dstParam[1] + dstStride[1]*srcSliceY/2;
121
122 copyPlane(src[0], srcStride[0], srcSliceY, srcSliceH, c->srcW,
123 dstParam[0], dstStride[0]);
124
125 if (c->dstFormat == PIX_FMT_NV12)
126 interleaveBytes(src[1], src[2], dst, c->srcW/2, srcSliceH/2, srcStride[1], srcStride[2], dstStride[0]);
127 else
128 interleaveBytes(src[2], src[1], dst, c->srcW/2, srcSliceH/2, srcStride[2], srcStride[1], dstStride[0]);
129
130 return srcSliceH;
131 }
132
133 static int planarToYuy2Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
134 int srcSliceH, uint8_t* dstParam[], int dstStride[])
135 {
136 uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
137
138 yv12toyuy2(src[0], src[1], src[2], dst, c->srcW, srcSliceH, srcStride[0], srcStride[1], dstStride[0]);
139
140 return srcSliceH;
141 }
142
143 static int planarToUyvyWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
144 int srcSliceH, uint8_t* dstParam[], int dstStride[])
145 {
146 uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
147
148 yv12touyvy(src[0], src[1], src[2], dst, c->srcW, srcSliceH, srcStride[0], srcStride[1], dstStride[0]);
149
150 return srcSliceH;
151 }
152
153 static int yuv422pToYuy2Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
154 int srcSliceH, uint8_t* dstParam[], int dstStride[])
155 {
156 uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
157
158 yuv422ptoyuy2(src[0],src[1],src[2],dst,c->srcW,srcSliceH,srcStride[0],srcStride[1],dstStride[0]);
159
160 return srcSliceH;
161 }
162
163 static int yuv422pToUyvyWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
164 int srcSliceH, uint8_t* dstParam[], int dstStride[])
165 {
166 uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
167
168 yuv422ptouyvy(src[0],src[1],src[2],dst,c->srcW,srcSliceH,srcStride[0],srcStride[1],dstStride[0]);
169
170 return srcSliceH;
171 }
172
173 static int yuyvToYuv420Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
174 int srcSliceH, uint8_t* dstParam[], int dstStride[])
175 {
176 uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
177 uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY/2;
178 uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY/2;
179
180 yuyvtoyuv420(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
181
182 if (dstParam[3])
183 fillPlane(dstParam[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
184
185 return srcSliceH;
186 }
187
188 static int yuyvToYuv422Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
189 int srcSliceH, uint8_t* dstParam[], int dstStride[])
190 {
191 uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
192 uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY;
193 uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY;
194
195 yuyvtoyuv422(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
196
197 return srcSliceH;
198 }
199
200 static int uyvyToYuv420Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
201 int srcSliceH, uint8_t* dstParam[], int dstStride[])
202 {
203 uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
204 uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY/2;
205 uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY/2;
206
207 uyvytoyuv420(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
208
209 if (dstParam[3])
210 fillPlane(dstParam[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
211
212 return srcSliceH;
213 }
214
215 static int uyvyToYuv422Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
216 int srcSliceH, uint8_t* dstParam[], int dstStride[])
217 {
218 uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
219 uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY;
220 uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY;
221
222 uyvytoyuv422(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
223
224 return srcSliceH;
225 }
226
227 static void gray8aToPacked32(const uint8_t *src, uint8_t *dst, int num_pixels, const uint8_t *palette)
228 {
229 int i;
230 for (i=0; i<num_pixels; i++)
231 ((uint32_t *) dst)[i] = ((const uint32_t *)palette)[src[i<<1]] | (src[(i<<1)+1] << 24);
232 }
233
234 static void gray8aToPacked32_1(const uint8_t *src, uint8_t *dst, int num_pixels, const uint8_t *palette)
235 {
236 int i;
237
238 for (i=0; i<num_pixels; i++)
239 ((uint32_t *) dst)[i] = ((const uint32_t *)palette)[src[i<<1]] | src[(i<<1)+1];
240 }
241
242 static void gray8aToPacked24(const uint8_t *src, uint8_t *dst, int num_pixels, const uint8_t *palette)
243 {
244 int i;
245
246 for (i=0; i<num_pixels; i++) {
247 //FIXME slow?
248 dst[0]= palette[src[i<<1]*4+0];
249 dst[1]= palette[src[i<<1]*4+1];
250 dst[2]= palette[src[i<<1]*4+2];
251 dst+= 3;
252 }
253 }
254
255 static int palToRgbWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
256 int srcSliceH, uint8_t* dst[], int dstStride[])
257 {
258 const enum PixelFormat srcFormat= c->srcFormat;
259 const enum PixelFormat dstFormat= c->dstFormat;
260 void (*conv)(const uint8_t *src, uint8_t *dst, int num_pixels,
261 const uint8_t *palette)=NULL;
262 int i;
263 uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
264 const uint8_t *srcPtr= src[0];
265
266 if (srcFormat == PIX_FMT_Y400A) {
267 switch (dstFormat) {
268 case PIX_FMT_RGB32 : conv = gray8aToPacked32; break;
269 case PIX_FMT_BGR32 : conv = gray8aToPacked32; break;
270 case PIX_FMT_BGR32_1: conv = gray8aToPacked32_1; break;
271 case PIX_FMT_RGB32_1: conv = gray8aToPacked32_1; break;
272 case PIX_FMT_RGB24 : conv = gray8aToPacked24; break;
273 case PIX_FMT_BGR24 : conv = gray8aToPacked24; break;
274 }
275 } else if (usePal(srcFormat)) {
276 switch (dstFormat) {
277 case PIX_FMT_RGB32 : conv = sws_convertPalette8ToPacked32; break;
278 case PIX_FMT_BGR32 : conv = sws_convertPalette8ToPacked32; break;
279 case PIX_FMT_BGR32_1: conv = sws_convertPalette8ToPacked32; break;
280 case PIX_FMT_RGB32_1: conv = sws_convertPalette8ToPacked32; break;
281 case PIX_FMT_RGB24 : conv = sws_convertPalette8ToPacked24; break;
282 case PIX_FMT_BGR24 : conv = sws_convertPalette8ToPacked24; break;
283 }
284 }
285
286 if (!conv)
287 av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
288 sws_format_name(srcFormat), sws_format_name(dstFormat));
289 else {
290 for (i=0; i<srcSliceH; i++) {
291 conv(srcPtr, dstPtr, c->srcW, (uint8_t *) c->pal_rgb);
292 srcPtr+= srcStride[0];
293 dstPtr+= dstStride[0];
294 }
295 }
296
297 return srcSliceH;
298 }
299
300 #define isRGBA32(x) ( \
301 (x) == PIX_FMT_ARGB \
302 || (x) == PIX_FMT_RGBA \
303 || (x) == PIX_FMT_BGRA \
304 || (x) == PIX_FMT_ABGR \
305 )
306
307 /* {RGB,BGR}{15,16,24,32,32_1} -> {RGB,BGR}{15,16,24,32} */
308 static int rgbToRgbWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
309 int srcSliceH, uint8_t* dst[], int dstStride[])
310 {
311 const enum PixelFormat srcFormat= c->srcFormat;
312 const enum PixelFormat dstFormat= c->dstFormat;
313 const int srcBpp= (c->srcFormatBpp + 7) >> 3;
314 const int dstBpp= (c->dstFormatBpp + 7) >> 3;
315 const int srcId= c->srcFormatBpp >> 2; /* 1:0, 4:1, 8:2, 15:3, 16:4, 24:6, 32:8 */
316 const int dstId= c->dstFormatBpp >> 2;
317 void (*conv)(const uint8_t *src, uint8_t *dst, int src_size)=NULL;
318
319 #define CONV_IS(src, dst) (srcFormat == PIX_FMT_##src && dstFormat == PIX_FMT_##dst)
320
321 if (isRGBA32(srcFormat) && isRGBA32(dstFormat)) {
322 if ( CONV_IS(ABGR, RGBA)
323 || CONV_IS(ARGB, BGRA)
324 || CONV_IS(BGRA, ARGB)
325 || CONV_IS(RGBA, ABGR)) conv = shuffle_bytes_3210;
326 else if (CONV_IS(ABGR, ARGB)
327 || CONV_IS(ARGB, ABGR)) conv = shuffle_bytes_0321;
328 else if (CONV_IS(ABGR, BGRA)
329 || CONV_IS(ARGB, RGBA)) conv = shuffle_bytes_1230;
330 else if (CONV_IS(BGRA, RGBA)
331 || CONV_IS(RGBA, BGRA)) conv = shuffle_bytes_2103;
332 else if (CONV_IS(BGRA, ABGR)
333 || CONV_IS(RGBA, ARGB)) conv = shuffle_bytes_3012;
334 } else
335 /* BGR -> BGR */
336 if ( (isBGRinInt(srcFormat) && isBGRinInt(dstFormat))
337 || (isRGBinInt(srcFormat) && isRGBinInt(dstFormat))) {
338 switch(srcId | (dstId<<4)) {
339 case 0x34: conv= rgb16to15; break;
340 case 0x36: conv= rgb24to15; break;
341 case 0x38: conv= rgb32to15; break;
342 case 0x43: conv= rgb15to16; break;
343 case 0x46: conv= rgb24to16; break;
344 case 0x48: conv= rgb32to16; break;
345 case 0x63: conv= rgb15to24; break;
346 case 0x64: conv= rgb16to24; break;
347 case 0x68: conv= rgb32to24; break;
348 case 0x83: conv= rgb15to32; break;
349 case 0x84: conv= rgb16to32; break;
350 case 0x86: conv= rgb24to32; break;
351 }
352 } else if ( (isBGRinInt(srcFormat) && isRGBinInt(dstFormat))
353 || (isRGBinInt(srcFormat) && isBGRinInt(dstFormat))) {
354 switch(srcId | (dstId<<4)) {
355 case 0x33: conv= rgb15tobgr15; break;
356 case 0x34: conv= rgb16tobgr15; break;
357 case 0x36: conv= rgb24tobgr15; break;
358 case 0x38: conv= rgb32tobgr15; break;
359 case 0x43: conv= rgb15tobgr16; break;
360 case 0x44: conv= rgb16tobgr16; break;
361 case 0x46: conv= rgb24tobgr16; break;
362 case 0x48: conv= rgb32tobgr16; break;
363 case 0x63: conv= rgb15tobgr24; break;
364 case 0x64: conv= rgb16tobgr24; break;
365 case 0x66: conv= rgb24tobgr24; break;
366 case 0x68: conv= rgb32tobgr24; break;
367 case 0x83: conv= rgb15tobgr32; break;
368 case 0x84: conv= rgb16tobgr32; break;
369 case 0x86: conv= rgb24tobgr32; break;
370 }
371 }
372
373 if (!conv) {
374 av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
375 sws_format_name(srcFormat), sws_format_name(dstFormat));
376 } else {
377 const uint8_t *srcPtr= src[0];
378 uint8_t *dstPtr= dst[0];
379 if ((srcFormat == PIX_FMT_RGB32_1 || srcFormat == PIX_FMT_BGR32_1) && !isRGBA32(dstFormat))
380 srcPtr += ALT32_CORR;
381
382 if ((dstFormat == PIX_FMT_RGB32_1 || dstFormat == PIX_FMT_BGR32_1) && !isRGBA32(srcFormat))
383 dstPtr += ALT32_CORR;
384
385 if (dstStride[0]*srcBpp == srcStride[0]*dstBpp && srcStride[0] > 0 && !(srcStride[0] % srcBpp))
386 conv(srcPtr, dstPtr + dstStride[0]*srcSliceY, srcSliceH*srcStride[0]);
387 else {
388 int i;
389 dstPtr += dstStride[0]*srcSliceY;
390
391 for (i=0; i<srcSliceH; i++) {
392 conv(srcPtr, dstPtr, c->srcW*srcBpp);
393 srcPtr+= srcStride[0];
394 dstPtr+= dstStride[0];
395 }
396 }
397 }
398 return srcSliceH;
399 }
400
401 static int bgr24ToYv12Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
402 int srcSliceH, uint8_t* dst[], int dstStride[])
403 {
404 rgb24toyv12(
405 src[0],
406 dst[0]+ srcSliceY *dstStride[0],
407 dst[1]+(srcSliceY>>1)*dstStride[1],
408 dst[2]+(srcSliceY>>1)*dstStride[2],
409 c->srcW, srcSliceH,
410 dstStride[0], dstStride[1], srcStride[0]);
411 if (dst[3])
412 fillPlane(dst[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
413 return srcSliceH;
414 }
415
416 static int yvu9ToYv12Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
417 int srcSliceH, uint8_t* dst[], int dstStride[])
418 {
419 copyPlane(src[0], srcStride[0], srcSliceY, srcSliceH, c->srcW,
420 dst[0], dstStride[0]);
421
422 planar2x(src[1], dst[1] + dstStride[1]*(srcSliceY >> 1), c->chrSrcW,
423 srcSliceH >> 2, srcStride[1], dstStride[1]);
424 planar2x(src[2], dst[2] + dstStride[2]*(srcSliceY >> 1), c->chrSrcW,
425 srcSliceH >> 2, srcStride[2], dstStride[2]);
426 if (dst[3])
427 fillPlane(dst[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
428 return srcSliceH;
429 }
430
431 /* unscaled copy like stuff (assumes nearly identical formats) */
432 static int packedCopyWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
433 int srcSliceH, uint8_t* dst[], int dstStride[])
434 {
435 if (dstStride[0]==srcStride[0] && srcStride[0] > 0)
436 memcpy(dst[0] + dstStride[0]*srcSliceY, src[0], srcSliceH*dstStride[0]);
437 else {
438 int i;
439 const uint8_t *srcPtr= src[0];
440 uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
441 int length=0;
442
443 /* universal length finder */
444 while(length+c->srcW <= FFABS(dstStride[0])
445 && length+c->srcW <= FFABS(srcStride[0])) length+= c->srcW;
446 assert(length!=0);
447
448 for (i=0; i<srcSliceH; i++) {
449 memcpy(dstPtr, srcPtr, length);
450 srcPtr+= srcStride[0];
451 dstPtr+= dstStride[0];
452 }
453 }
454 return srcSliceH;
455 }
456
457 #define clip9(x) av_clip_uintp2(x, 9)
458 #define clip10(x) av_clip_uintp2(x, 10)
459 #define DITHER_COPY(dst, dstStride, wfunc, src, srcStride, rfunc, dithers, shift, clip) \
460 for (i = 0; i < height; i++) { \
461 const uint8_t *dither = dithers[i & 7]; \
462 for (j = 0; j < length - 7; j += 8) { \
463 wfunc(&dst[j + 0], clip((rfunc(&src[j + 0]) + dither[0]) >> shift)); \
464 wfunc(&dst[j + 1], clip((rfunc(&src[j + 1]) + dither[1]) >> shift)); \
465 wfunc(&dst[j + 2], clip((rfunc(&src[j + 2]) + dither[2]) >> shift)); \
466 wfunc(&dst[j + 3], clip((rfunc(&src[j + 3]) + dither[3]) >> shift)); \
467 wfunc(&dst[j + 4], clip((rfunc(&src[j + 4]) + dither[4]) >> shift)); \
468 wfunc(&dst[j + 5], clip((rfunc(&src[j + 5]) + dither[5]) >> shift)); \
469 wfunc(&dst[j + 6], clip((rfunc(&src[j + 6]) + dither[6]) >> shift)); \
470 wfunc(&dst[j + 7], clip((rfunc(&src[j + 7]) + dither[7]) >> shift)); \
471 } \
472 for (; j < length; j++) \
473 wfunc(&dst[j], (rfunc(&src[j]) + dither[j & 7]) >> shift); \
474 dst += dstStride; \
475 src += srcStride; \
476 }
477
478 static int planarCopyWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
479 int srcSliceH, uint8_t* dst[], int dstStride[])
480 {
481 int plane, i, j;
482 for (plane=0; plane<4; plane++) {
483 int length= (plane==0 || plane==3) ? c->srcW : -((-c->srcW )>>c->chrDstHSubSample);
484 int y= (plane==0 || plane==3) ? srcSliceY: -((-srcSliceY)>>c->chrDstVSubSample);
485 int height= (plane==0 || plane==3) ? srcSliceH: -((-srcSliceH)>>c->chrDstVSubSample);
486 const uint8_t *srcPtr= src[plane];
487 uint8_t *dstPtr= dst[plane] + dstStride[plane]*y;
488
489 if (!dst[plane]) continue;
490 // ignore palette for GRAY8
491 if (plane == 1 && !dst[2]) continue;
492 if (!src[plane] || (plane == 1 && !src[2])) {
493 if(is16BPS(c->dstFormat))
494 length*=2;
495 fillPlane(dst[plane], dstStride[plane], length, height, y, (plane==3) ? 255 : 128);
496 } else {
497 if(is9_OR_10BPS(c->srcFormat)) {
498 const int src_depth = av_pix_fmt_descriptors[c->srcFormat].comp[plane].depth_minus1+1;
499 const int dst_depth = av_pix_fmt_descriptors[c->dstFormat].comp[plane].depth_minus1+1;
500 const uint16_t *srcPtr2 = (const uint16_t*)srcPtr;
501
502 if (is16BPS(c->dstFormat)) {
503 uint16_t *dstPtr2 = (uint16_t*)dstPtr;
504 #define COPY9_OR_10TO16(rfunc, wfunc) \
505 for (i = 0; i < height; i++) { \
506 for (j = 0; j < length; j++) { \
507 int srcpx = rfunc(&srcPtr2[j]); \
508 wfunc(&dstPtr2[j], (srcpx<<(16-src_depth)) | (srcpx>>(2*src_depth-16))); \
509 } \
510 dstPtr2 += dstStride[plane]/2; \
511 srcPtr2 += srcStride[plane]/2; \
512 }
513 if (isBE(c->dstFormat)) {
514 if (isBE(c->srcFormat)) {
515 COPY9_OR_10TO16(AV_RB16, AV_WB16);
516 } else {
517 COPY9_OR_10TO16(AV_RL16, AV_WB16);
518 }
519 } else {
520 if (isBE(c->srcFormat)) {
521 COPY9_OR_10TO16(AV_RB16, AV_WL16);
522 } else {
523 COPY9_OR_10TO16(AV_RL16, AV_WL16);
524 }
525 }
526 } else if (is9_OR_10BPS(c->dstFormat)) {
527 uint16_t *dstPtr2 = (uint16_t*)dstPtr;
528 #define COPY9_OR_10TO9_OR_10(loop) \
529 for (i = 0; i < height; i++) { \
530 for (j = 0; j < length; j++) { \
531 loop; \
532 } \
533 dstPtr2 += dstStride[plane]/2; \
534 srcPtr2 += srcStride[plane]/2; \
535 }
536 #define COPY9_OR_10TO9_OR_10_2(rfunc, wfunc) \
537 if (dst_depth > src_depth) { \
538 COPY9_OR_10TO9_OR_10(int srcpx = rfunc(&srcPtr2[j]); \
539 wfunc(&dstPtr2[j], (srcpx << 1) | (srcpx >> 9))); \
540 } else if (dst_depth < src_depth) { \
541 DITHER_COPY(dstPtr2, dstStride[plane]/2, wfunc, \
542 srcPtr2, srcStride[plane]/2, rfunc, \
543 dither_8x8_1, 1, clip9); \
544 } else { \
545 COPY9_OR_10TO9_OR_10(wfunc(&dstPtr2[j], rfunc(&srcPtr2[j]))); \
546 }
547 if (isBE(c->dstFormat)) {
548 if (isBE(c->srcFormat)) {
549 COPY9_OR_10TO9_OR_10_2(AV_RB16, AV_WB16);
550 } else {
551 COPY9_OR_10TO9_OR_10_2(AV_RL16, AV_WB16);
552 }
553 } else {
554 if (isBE(c->srcFormat)) {
555 COPY9_OR_10TO9_OR_10_2(AV_RB16, AV_WL16);
556 } else {
557 COPY9_OR_10TO9_OR_10_2(AV_RL16, AV_WL16);
558 }
559 }
560 } else {
561 #define W8(a, b) { *(a) = (b); }
562 #define COPY9_OR_10TO8(rfunc) \
563 if (src_depth == 9) { \
564 DITHER_COPY(dstPtr, dstStride[plane], W8, \
565 srcPtr2, srcStride[plane]/2, rfunc, \
566 dither_8x8_1, 1, av_clip_uint8); \
567 } else { \
568 DITHER_COPY(dstPtr, dstStride[plane], W8, \
569 srcPtr2, srcStride[plane]/2, rfunc, \
570 dither_8x8_3, 2, av_clip_uint8); \
571 }
572 if (isBE(c->srcFormat)) {
573 COPY9_OR_10TO8(AV_RB16);
574 } else {
575 COPY9_OR_10TO8(AV_RL16);
576 }
577 }
578 } else if(is9_OR_10BPS(c->dstFormat)) {
579 const int dst_depth = av_pix_fmt_descriptors[c->dstFormat].comp[plane].depth_minus1+1;
580 uint16_t *dstPtr2 = (uint16_t*)dstPtr;
581
582 if (is16BPS(c->srcFormat)) {
583 const uint16_t *srcPtr2 = (const uint16_t*)srcPtr;
584 #define COPY16TO9_OR_10(rfunc, wfunc) \
585 if (dst_depth == 9) { \
586 DITHER_COPY(dstPtr2, dstStride[plane]/2, wfunc, \
587 srcPtr2, srcStride[plane]/2, rfunc, \
588 dither_8x8_128, 7, clip9); \
589 } else { \
590 DITHER_COPY(dstPtr2, dstStride[plane]/2, wfunc, \
591 srcPtr2, srcStride[plane]/2, rfunc, \
592 dither_8x8_64, 6, clip10); \
593 }
594 if (isBE(c->dstFormat)) {
595 if (isBE(c->srcFormat)) {
596 COPY16TO9_OR_10(AV_RB16, AV_WB16);
597 } else {
598 COPY16TO9_OR_10(AV_RL16, AV_WB16);
599 }
600 } else {
601 if (isBE(c->srcFormat)) {
602 COPY16TO9_OR_10(AV_RB16, AV_WL16);
603 } else {
604 COPY16TO9_OR_10(AV_RL16, AV_WL16);
605 }
606 }
607 } else /* 8bit */ {
608 #define COPY8TO9_OR_10(wfunc) \
609 for (i = 0; i < height; i++) { \
610 for (j = 0; j < length; j++) { \
611 const int srcpx = srcPtr[j]; \
612 wfunc(&dstPtr2[j], (srcpx<<(dst_depth-8)) | (srcpx >> (16-dst_depth))); \
613 } \
614 dstPtr2 += dstStride[plane]/2; \
615 srcPtr += srcStride[plane]; \
616 }
617 if (isBE(c->dstFormat)) {
618 COPY8TO9_OR_10(AV_WB16);
619 } else {
620 COPY8TO9_OR_10(AV_WL16);
621 }
622 }
623 } else if(is16BPS(c->srcFormat) && !is16BPS(c->dstFormat)) {
624 const uint16_t *srcPtr2 = (const uint16_t*)srcPtr;
625 #define COPY16TO8(rfunc) \
626 DITHER_COPY(dstPtr, dstStride[plane], W8, \
627 srcPtr2, srcStride[plane]/2, rfunc, \
628 dither_8x8_256, 8, av_clip_uint8);
629 if (isBE(c->srcFormat)) {
630 COPY16TO8(AV_RB16);
631 } else {
632 COPY16TO8(AV_RL16);
633 }
634 } else if(!is16BPS(c->srcFormat) && is16BPS(c->dstFormat)) {
635 for (i=0; i<height; i++) {
636 for (j=0; j<length; j++) {
637 dstPtr[ j<<1 ] = srcPtr[j];
638 dstPtr[(j<<1)+1] = srcPtr[j];
639 }
640 srcPtr+= srcStride[plane];
641 dstPtr+= dstStride[plane];
642 }
643 } else if(is16BPS(c->srcFormat) && is16BPS(c->dstFormat)
644 && isBE(c->srcFormat) != isBE(c->dstFormat)) {
645
646 for (i=0; i<height; i++) {
647 for (j=0; j<length; j++)
648 ((uint16_t*)dstPtr)[j] = av_bswap16(((const uint16_t*)srcPtr)[j]);
649 srcPtr+= srcStride[plane];
650 dstPtr+= dstStride[plane];
651 }
652 } else if (dstStride[plane] == srcStride[plane] &&
653 srcStride[plane] > 0 && srcStride[plane] == length) {
654 memcpy(dst[plane] + dstStride[plane]*y, src[plane],
655 height*dstStride[plane]);
656 } else {
657 if(is16BPS(c->srcFormat) && is16BPS(c->dstFormat))
658 length*=2;
659 for (i=0; i<height; i++) {
660 memcpy(dstPtr, srcPtr, length);
661 srcPtr+= srcStride[plane];
662 dstPtr+= dstStride[plane];
663 }
664 }
665 }
666 }
667 return srcSliceH;
668 }
669
670 void ff_get_unscaled_swscale(SwsContext *c)
671 {
672 const enum PixelFormat srcFormat = c->srcFormat;
673 const enum PixelFormat dstFormat = c->dstFormat;
674 const int flags = c->flags;
675 const int dstH = c->dstH;
676 int needsDither;
677
678 needsDither= isAnyRGB(dstFormat)
679 && c->dstFormatBpp < 24
680 && (c->dstFormatBpp < c->srcFormatBpp || (!isAnyRGB(srcFormat)));
681
682 /* yv12_to_nv12 */
683 if ((srcFormat == PIX_FMT_YUV420P || srcFormat == PIX_FMT_YUVA420P) && (dstFormat == PIX_FMT_NV12 || dstFormat == PIX_FMT_NV21)) {
684 c->swScale= planarToNv12Wrapper;
685 }
686 /* yuv2bgr */
687 if ((srcFormat==PIX_FMT_YUV420P || srcFormat==PIX_FMT_YUV422P || srcFormat==PIX_FMT_YUVA420P) && isAnyRGB(dstFormat)
688 && !(flags & SWS_ACCURATE_RND) && !(dstH&1)) {
689 c->swScale= ff_yuv2rgb_get_func_ptr(c);
690 }
691
692 if (srcFormat==PIX_FMT_YUV410P && (dstFormat==PIX_FMT_YUV420P || dstFormat==PIX_FMT_YUVA420P) && !(flags & SWS_BITEXACT)) {
693 c->swScale= yvu9ToYv12Wrapper;
694 }
695
696 /* bgr24toYV12 */
697 if (srcFormat==PIX_FMT_BGR24 && (dstFormat==PIX_FMT_YUV420P || dstFormat==PIX_FMT_YUVA420P) && !(flags & SWS_ACCURATE_RND))
698 c->swScale= bgr24ToYv12Wrapper;
699
700 /* RGB/BGR -> RGB/BGR (no dither needed forms) */
701 if ( isAnyRGB(srcFormat)
702 && isAnyRGB(dstFormat)
703 && srcFormat != PIX_FMT_BGR8 && dstFormat != PIX_FMT_BGR8
704 && srcFormat != PIX_FMT_RGB8 && dstFormat != PIX_FMT_RGB8
705 && srcFormat != PIX_FMT_BGR4 && dstFormat != PIX_FMT_BGR4
706 && srcFormat != PIX_FMT_RGB4 && dstFormat != PIX_FMT_RGB4
707 && srcFormat != PIX_FMT_BGR4_BYTE && dstFormat != PIX_FMT_BGR4_BYTE
708 && srcFormat != PIX_FMT_RGB4_BYTE && dstFormat != PIX_FMT_RGB4_BYTE
709 && srcFormat != PIX_FMT_MONOBLACK && dstFormat != PIX_FMT_MONOBLACK
710 && srcFormat != PIX_FMT_MONOWHITE && dstFormat != PIX_FMT_MONOWHITE
711 && srcFormat != PIX_FMT_RGB48LE && dstFormat != PIX_FMT_RGB48LE
712 && srcFormat != PIX_FMT_RGB48BE && dstFormat != PIX_FMT_RGB48BE
713 && srcFormat != PIX_FMT_BGR48LE && dstFormat != PIX_FMT_BGR48LE
714 && srcFormat != PIX_FMT_BGR48BE && dstFormat != PIX_FMT_BGR48BE
715 && (!needsDither || (c->flags&(SWS_FAST_BILINEAR|SWS_POINT))))
716 c->swScale= rgbToRgbWrapper;
717
718 if ((usePal(srcFormat) && (
719 dstFormat == PIX_FMT_RGB32 ||
720 dstFormat == PIX_FMT_RGB32_1 ||
721 dstFormat == PIX_FMT_RGB24 ||
722 dstFormat == PIX_FMT_BGR32 ||
723 dstFormat == PIX_FMT_BGR32_1 ||
724 dstFormat == PIX_FMT_BGR24)))
725 c->swScale= palToRgbWrapper;
726
727 if (srcFormat == PIX_FMT_YUV422P) {
728 if (dstFormat == PIX_FMT_YUYV422)
729 c->swScale= yuv422pToYuy2Wrapper;
730 else if (dstFormat == PIX_FMT_UYVY422)
731 c->swScale= yuv422pToUyvyWrapper;
732 }
733
734 /* LQ converters if -sws 0 or -sws 4*/
735 if (c->flags&(SWS_FAST_BILINEAR|SWS_POINT)) {
736 /* yv12_to_yuy2 */
737 if (srcFormat == PIX_FMT_YUV420P || srcFormat == PIX_FMT_YUVA420P) {
738 if (dstFormat == PIX_FMT_YUYV422)
739 c->swScale= planarToYuy2Wrapper;
740 else if (dstFormat == PIX_FMT_UYVY422)
741 c->swScale= planarToUyvyWrapper;
742 }
743 }
744 if(srcFormat == PIX_FMT_YUYV422 && (dstFormat == PIX_FMT_YUV420P || dstFormat == PIX_FMT_YUVA420P))
745 c->swScale= yuyvToYuv420Wrapper;
746 if(srcFormat == PIX_FMT_UYVY422 && (dstFormat == PIX_FMT_YUV420P || dstFormat == PIX_FMT_YUVA420P))
747 c->swScale= uyvyToYuv420Wrapper;
748 if(srcFormat == PIX_FMT_YUYV422 && dstFormat == PIX_FMT_YUV422P)
749 c->swScale= yuyvToYuv422Wrapper;
750 if(srcFormat == PIX_FMT_UYVY422 && dstFormat == PIX_FMT_YUV422P)
751 c->swScale= uyvyToYuv422Wrapper;
752
753 /* simple copy */
754 if ( srcFormat == dstFormat
755 || (srcFormat == PIX_FMT_YUVA420P && dstFormat == PIX_FMT_YUV420P)
756 || (srcFormat == PIX_FMT_YUV420P && dstFormat == PIX_FMT_YUVA420P)
757 || (isPlanarYUV(srcFormat) && isGray(dstFormat))
758 || (isPlanarYUV(dstFormat) && isGray(srcFormat))
759 || (isGray(dstFormat) && isGray(srcFormat))
760 || (isPlanarYUV(srcFormat) && isPlanarYUV(dstFormat)
761 && c->chrDstHSubSample == c->chrSrcHSubSample
762 && c->chrDstVSubSample == c->chrSrcVSubSample
763 && dstFormat != PIX_FMT_NV12 && dstFormat != PIX_FMT_NV21
764 && srcFormat != PIX_FMT_NV12 && srcFormat != PIX_FMT_NV21))
765 {
766 if (isPacked(c->srcFormat))
767 c->swScale= packedCopyWrapper;
768 else /* Planar YUV or gray */
769 c->swScale= planarCopyWrapper;
770 }
771
772 if (ARCH_BFIN)
773 ff_bfin_get_unscaled_swscale(c);
774 if (HAVE_ALTIVEC)
775 ff_swscale_get_unscaled_altivec(c);
776 }
777
778 static void reset_ptr(const uint8_t* src[], int format)
779 {
780 if(!isALPHA(format))
781 src[3]=NULL;
782 if(!isPlanarYUV(format)) {
783 src[3]=src[2]=NULL;
784
785 if (!usePal(format))
786 src[1]= NULL;
787 }
788 }
789
790 static int check_image_pointers(uint8_t *data[4], enum PixelFormat pix_fmt,
791 const int linesizes[4])
792 {
793 const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
794 int i;
795
796 for (i = 0; i < 4; i++) {
797 int plane = desc->comp[i].plane;
798 if (!data[plane] || !linesizes[plane])
799 return 0;
800 }
801
802 return 1;
803 }
804
805 /**
806 * swscale wrapper, so we don't need to export the SwsContext.
807 * Assumes planar YUV to be in YUV order instead of YVU.
808 */
809 int attribute_align_arg sws_scale(struct SwsContext *c, const uint8_t* const srcSlice[],
810 const int srcStride[], int srcSliceY, int srcSliceH,
811 uint8_t* const dst[], const int dstStride[])
812 {
813 int i;
814 const uint8_t* src2[4]= {srcSlice[0], srcSlice[1], srcSlice[2], srcSlice[3]};
815 uint8_t* dst2[4]= {dst[0], dst[1], dst[2], dst[3]};
816
817 // do not mess up sliceDir if we have a "trailing" 0-size slice
818 if (srcSliceH == 0)
819 return 0;
820
821 if (!check_image_pointers(srcSlice, c->srcFormat, srcStride)) {
822 av_log(c, AV_LOG_ERROR, "bad src image pointers\n");
823 return 0;
824 }
825 if (!check_image_pointers(dst, c->dstFormat, dstStride)) {
826 av_log(c, AV_LOG_ERROR, "bad dst image pointers\n");
827 return 0;
828 }
829
830 if (c->sliceDir == 0 && srcSliceY != 0 && srcSliceY + srcSliceH != c->srcH) {
831 av_log(c, AV_LOG_ERROR, "Slices start in the middle!\n");
832 return 0;
833 }
834 if (c->sliceDir == 0) {
835 if (srcSliceY == 0) c->sliceDir = 1; else c->sliceDir = -1;
836 }
837
838 if (usePal(c->srcFormat)) {
839 for (i=0; i<256; i++) {
840 int p, r, g, b,y,u,v;
841 if(c->srcFormat == PIX_FMT_PAL8) {
842 p=((const uint32_t*)(srcSlice[1]))[i];
843 r= (p>>16)&0xFF;
844 g= (p>> 8)&0xFF;
845 b= p &0xFF;
846 } else if(c->srcFormat == PIX_FMT_RGB8) {
847 r= (i>>5 )*36;
848 g= ((i>>2)&7)*36;
849 b= (i&3 )*85;
850 } else if(c->srcFormat == PIX_FMT_BGR8) {
851 b= (i>>6 )*85;
852 g= ((i>>3)&7)*36;
853 r= (i&7 )*36;
854 } else if(c->srcFormat == PIX_FMT_RGB4_BYTE) {
855 r= (i>>3 )*255;
856 g= ((i>>1)&3)*85;
857 b= (i&1 )*255;
858 } else if(c->srcFormat == PIX_FMT_GRAY8 || c->srcFormat == PIX_FMT_Y400A) {
859 r = g = b = i;
860 } else {
861 assert(c->srcFormat == PIX_FMT_BGR4_BYTE);
862 b= (i>>3 )*255;
863 g= ((i>>1)&3)*85;
864 r= (i&1 )*255;
865 }
866 y= av_clip_uint8((RY*r + GY*g + BY*b + ( 33<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
867 u= av_clip_uint8((RU*r + GU*g + BU*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
868 v= av_clip_uint8((RV*r + GV*g + BV*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
869 c->pal_yuv[i]= y + (u<<8) + (v<<16);
870
871 switch(c->dstFormat) {
872 case PIX_FMT_BGR32:
873 #if !HAVE_BIGENDIAN
874 case PIX_FMT_RGB24:
875 #endif
876 c->pal_rgb[i]= r + (g<<8) + (b<<16);
877 break;
878 case PIX_FMT_BGR32_1:
879 #if HAVE_BIGENDIAN
880 case PIX_FMT_BGR24:
881 #endif
882 c->pal_rgb[i]= (r + (g<<8) + (b<<16)) << 8;
883 break;
884 case PIX_FMT_RGB32_1:
885 #if HAVE_BIGENDIAN
886 case PIX_FMT_RGB24:
887 #endif
888 c->pal_rgb[i]= (b + (g<<8) + (r<<16)) << 8;
889 break;
890 case PIX_FMT_RGB32:
891 #if !HAVE_BIGENDIAN
892 case PIX_FMT_BGR24:
893 #endif
894 default:
895 c->pal_rgb[i]= b + (g<<8) + (r<<16);
896 }
897 }
898 }
899
900 // copy strides, so they can safely be modified
901 if (c->sliceDir == 1) {
902 // slices go from top to bottom
903 int srcStride2[4]= {srcStride[0], srcStride[1], srcStride[2], srcStride[3]};
904 int dstStride2[4]= {dstStride[0], dstStride[1], dstStride[2], dstStride[3]};
905
906 reset_ptr(src2, c->srcFormat);
907 reset_ptr((const uint8_t**)dst2, c->dstFormat);
908
909 /* reset slice direction at end of frame */
910 if (srcSliceY + srcSliceH == c->srcH)
911 c->sliceDir = 0;
912
913 return c->swScale(c, src2, srcStride2, srcSliceY, srcSliceH, dst2, dstStride2);
914 } else {
915 // slices go from bottom to top => we flip the image internally
916 int srcStride2[4]= {-srcStride[0], -srcStride[1], -srcStride[2], -srcStride[3]};
917 int dstStride2[4]= {-dstStride[0], -dstStride[1], -dstStride[2], -dstStride[3]};
918
919 src2[0] += (srcSliceH-1)*srcStride[0];
920 if (!usePal(c->srcFormat))
921 src2[1] += ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[1];
922 src2[2] += ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[2];
923 src2[3] += (srcSliceH-1)*srcStride[3];
924 dst2[0] += ( c->dstH -1)*dstStride[0];
925 dst2[1] += ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[1];
926 dst2[2] += ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[2];
927 dst2[3] += ( c->dstH -1)*dstStride[3];
928
929 reset_ptr(src2, c->srcFormat);
930 reset_ptr((const uint8_t**)dst2, c->dstFormat);
931
932 /* reset slice direction at end of frame */
933 if (!srcSliceY)
934 c->sliceDir = 0;
935
936 return c->swScale(c, src2, srcStride2, c->srcH-srcSliceY-srcSliceH, srcSliceH, dst2, dstStride2);
937 }
938 }
939
940 /* Convert the palette to the same packed 32-bit format as the palette */
941 void sws_convertPalette8ToPacked32(const uint8_t *src, uint8_t *dst, int num_pixels, const uint8_t *palette)
942 {
943 int i;
944
945 for (i=0; i<num_pixels; i++)
946 ((uint32_t *) dst)[i] = ((const uint32_t *) palette)[src[i]];
947 }
948
949 /* Palette format: ABCD -> dst format: ABC */
950 void sws_convertPalette8ToPacked24(const uint8_t *src, uint8_t *dst, int num_pixels, const uint8_t *palette)
951 {
952 int i;
953
954 for (i=0; i<num_pixels; i++) {
955 //FIXME slow?
956 dst[0]= palette[src[i]*4+0];
957 dst[1]= palette[src[i]*4+1];
958 dst[2]= palette[src[i]*4+2];
959 dst+= 3;
960 }
961 }