00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <inttypes.h>
00022 #include <string.h>
00023 #include <math.h>
00024 #include <stdio.h>
00025 #include "config.h"
00026 #include <assert.h>
00027 #include "swscale.h"
00028 #include "swscale_internal.h"
00029 #include "rgb2rgb.h"
00030 #include "libavutil/intreadwrite.h"
00031 #include "libavutil/cpu.h"
00032 #include "libavutil/avutil.h"
00033 #include "libavutil/mathematics.h"
00034 #include "libavutil/bswap.h"
00035 #include "libavutil/pixdesc.h"
00036
00037 #define RGB2YUV_SHIFT 15
00038 #define BY ( (int) (0.114 * 219 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
00039 #define BV (-(int) (0.081 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
00040 #define BU ( (int) (0.500 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
00041 #define GY ( (int) (0.587 * 219 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
00042 #define GV (-(int) (0.419 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
00043 #define GU (-(int) (0.331 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
00044 #define RY ( (int) (0.299 * 219 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
00045 #define RV ( (int) (0.500 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
00046 #define RU (-(int) (0.169 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
00047
00048 DECLARE_ALIGNED(8, const uint8_t, dithers)[8][8][8]={
00049 {
00050 { 0, 1, 0, 1, 0, 1, 0, 1,},
00051 { 1, 0, 1, 0, 1, 0, 1, 0,},
00052 { 0, 1, 0, 1, 0, 1, 0, 1,},
00053 { 1, 0, 1, 0, 1, 0, 1, 0,},
00054 { 0, 1, 0, 1, 0, 1, 0, 1,},
00055 { 1, 0, 1, 0, 1, 0, 1, 0,},
00056 { 0, 1, 0, 1, 0, 1, 0, 1,},
00057 { 1, 0, 1, 0, 1, 0, 1, 0,},
00058 },{
00059 { 1, 2, 1, 2, 1, 2, 1, 2,},
00060 { 3, 0, 3, 0, 3, 0, 3, 0,},
00061 { 1, 2, 1, 2, 1, 2, 1, 2,},
00062 { 3, 0, 3, 0, 3, 0, 3, 0,},
00063 { 1, 2, 1, 2, 1, 2, 1, 2,},
00064 { 3, 0, 3, 0, 3, 0, 3, 0,},
00065 { 1, 2, 1, 2, 1, 2, 1, 2,},
00066 { 3, 0, 3, 0, 3, 0, 3, 0,},
00067 },{
00068 { 2, 4, 3, 5, 2, 4, 3, 5,},
00069 { 6, 0, 7, 1, 6, 0, 7, 1,},
00070 { 3, 5, 2, 4, 3, 5, 2, 4,},
00071 { 7, 1, 6, 0, 7, 1, 6, 0,},
00072 { 2, 4, 3, 5, 2, 4, 3, 5,},
00073 { 6, 0, 7, 1, 6, 0, 7, 1,},
00074 { 3, 5, 2, 4, 3, 5, 2, 4,},
00075 { 7, 1, 6, 0, 7, 1, 6, 0,},
00076 },{
00077 { 4, 8, 7, 11, 4, 8, 7, 11,},
00078 { 12, 0, 15, 3, 12, 0, 15, 3,},
00079 { 6, 10, 5, 9, 6, 10, 5, 9,},
00080 { 14, 2, 13, 1, 14, 2, 13, 1,},
00081 { 4, 8, 7, 11, 4, 8, 7, 11,},
00082 { 12, 0, 15, 3, 12, 0, 15, 3,},
00083 { 6, 10, 5, 9, 6, 10, 5, 9,},
00084 { 14, 2, 13, 1, 14, 2, 13, 1,},
00085 },{
00086 { 9, 17, 15, 23, 8, 16, 14, 22,},
00087 { 25, 1, 31, 7, 24, 0, 30, 6,},
00088 { 13, 21, 11, 19, 12, 20, 10, 18,},
00089 { 29, 5, 27, 3, 28, 4, 26, 2,},
00090 { 8, 16, 14, 22, 9, 17, 15, 23,},
00091 { 24, 0, 30, 6, 25, 1, 31, 7,},
00092 { 12, 20, 10, 18, 13, 21, 11, 19,},
00093 { 28, 4, 26, 2, 29, 5, 27, 3,},
00094 },{
00095 { 18, 34, 30, 46, 17, 33, 29, 45,},
00096 { 50, 2, 62, 14, 49, 1, 61, 13,},
00097 { 26, 42, 22, 38, 25, 41, 21, 37,},
00098 { 58, 10, 54, 6, 57, 9, 53, 5,},
00099 { 16, 32, 28, 44, 19, 35, 31, 47,},
00100 { 48, 0, 60, 12, 51, 3, 63, 15,},
00101 { 24, 40, 20, 36, 27, 43, 23, 39,},
00102 { 56, 8, 52, 4, 59, 11, 55, 7,},
00103 },{
00104 { 18, 34, 30, 46, 17, 33, 29, 45,},
00105 { 50, 2, 62, 14, 49, 1, 61, 13,},
00106 { 26, 42, 22, 38, 25, 41, 21, 37,},
00107 { 58, 10, 54, 6, 57, 9, 53, 5,},
00108 { 16, 32, 28, 44, 19, 35, 31, 47,},
00109 { 48, 0, 60, 12, 51, 3, 63, 15,},
00110 { 24, 40, 20, 36, 27, 43, 23, 39,},
00111 { 56, 8, 52, 4, 59, 11, 55, 7,},
00112 },{
00113 { 36, 68, 60, 92, 34, 66, 58, 90,},
00114 { 100, 4,124, 28, 98, 2,122, 26,},
00115 { 52, 84, 44, 76, 50, 82, 42, 74,},
00116 { 116, 20,108, 12,114, 18,106, 10,},
00117 { 32, 64, 56, 88, 38, 70, 62, 94,},
00118 { 96, 0,120, 24,102, 6,126, 30,},
00119 { 48, 80, 40, 72, 54, 86, 46, 78,},
00120 { 112, 16,104, 8,118, 22,110, 14,},
00121 }};
00122
00123 static const uint8_t flat64[8]={64,64,64,64,64,64,64,64};
00124
00125 const uint16_t dither_scale[15][16]={
00126 { 2, 3, 3, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,},
00127 { 2, 3, 7, 7, 13, 13, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25,},
00128 { 3, 3, 4, 15, 15, 29, 57, 57, 57, 113, 113, 113, 113, 113, 113, 113,},
00129 { 3, 4, 4, 5, 31, 31, 61, 121, 241, 241, 241, 241, 481, 481, 481, 481,},
00130 { 3, 4, 5, 5, 6, 63, 63, 125, 249, 497, 993, 993, 993, 993, 993, 1985,},
00131 { 3, 5, 6, 6, 6, 7, 127, 127, 253, 505, 1009, 2017, 4033, 4033, 4033, 4033,},
00132 { 3, 5, 6, 7, 7, 7, 8, 255, 255, 509, 1017, 2033, 4065, 8129,16257,16257,},
00133 { 3, 5, 6, 8, 8, 8, 8, 9, 511, 511, 1021, 2041, 4081, 8161,16321,32641,},
00134 { 3, 5, 7, 8, 9, 9, 9, 9, 10, 1023, 1023, 2045, 4089, 8177,16353,32705,},
00135 { 3, 5, 7, 8, 10, 10, 10, 10, 10, 11, 2047, 2047, 4093, 8185,16369,32737,},
00136 { 3, 5, 7, 8, 10, 11, 11, 11, 11, 11, 12, 4095, 4095, 8189,16377,32753,},
00137 { 3, 5, 7, 9, 10, 12, 12, 12, 12, 12, 12, 13, 8191, 8191,16381,32761,},
00138 { 3, 5, 7, 9, 10, 12, 13, 13, 13, 13, 13, 13, 14,16383,16383,32765,},
00139 { 3, 5, 7, 9, 10, 12, 14, 14, 14, 14, 14, 14, 14, 15,32767,32767,},
00140 { 3, 5, 7, 9, 11, 12, 14, 15, 15, 15, 15, 15, 15, 15, 16,65535,},
00141 };
00142
00143
00144 static void fillPlane(uint8_t *plane, int stride, int width, int height, int y,
00145 uint8_t val)
00146 {
00147 int i;
00148 uint8_t *ptr = plane + stride * y;
00149 for (i = 0; i < height; i++) {
00150 memset(ptr, val, width);
00151 ptr += stride;
00152 }
00153 }
00154
00155 static void fillPlane16(uint8_t *plane, int stride, int width, int height, int y,
00156 int alpha, int bits)
00157 {
00158 int i, j;
00159 uint8_t *ptr = plane + stride * y;
00160 int v = alpha ? -1 : (1<<bits);
00161 for (i = 0; i < height; i++) {
00162 for (j = 0; j < width; j++) {
00163 AV_WN16(ptr+2*j, v);
00164 }
00165 ptr += stride;
00166 }
00167 }
00168
00169 static void copyPlane(const uint8_t *src, int srcStride,
00170 int srcSliceY, int srcSliceH, int width,
00171 uint8_t *dst, int dstStride)
00172 {
00173 dst += dstStride * srcSliceY;
00174 if (dstStride == srcStride && srcStride > 0) {
00175 memcpy(dst, src, srcSliceH * dstStride);
00176 } else {
00177 int i;
00178 for (i = 0; i < srcSliceH; i++) {
00179 memcpy(dst, src, width);
00180 src += srcStride;
00181 dst += dstStride;
00182 }
00183 }
00184 }
00185
00186 static int planarToNv12Wrapper(SwsContext *c, const uint8_t *src[],
00187 int srcStride[], int srcSliceY,
00188 int srcSliceH, uint8_t *dstParam[],
00189 int dstStride[])
00190 {
00191 uint8_t *dst = dstParam[1] + dstStride[1] * srcSliceY / 2;
00192
00193 copyPlane(src[0], srcStride[0], srcSliceY, srcSliceH, c->srcW,
00194 dstParam[0], dstStride[0]);
00195
00196 if (c->dstFormat == PIX_FMT_NV12)
00197 interleaveBytes(src[1], src[2], dst, c->srcW / 2, srcSliceH / 2,
00198 srcStride[1], srcStride[2], dstStride[0]);
00199 else
00200 interleaveBytes(src[2], src[1], dst, c->srcW / 2, srcSliceH / 2,
00201 srcStride[2], srcStride[1], dstStride[0]);
00202
00203 return srcSliceH;
00204 }
00205
00206 static int planarToYuy2Wrapper(SwsContext *c, const uint8_t *src[],
00207 int srcStride[], int srcSliceY, int srcSliceH,
00208 uint8_t *dstParam[], int dstStride[])
00209 {
00210 uint8_t *dst = dstParam[0] + dstStride[0] * srcSliceY;
00211
00212 yv12toyuy2(src[0], src[1], src[2], dst, c->srcW, srcSliceH, srcStride[0],
00213 srcStride[1], dstStride[0]);
00214
00215 return srcSliceH;
00216 }
00217
00218 static int planarToUyvyWrapper(SwsContext *c, const uint8_t *src[],
00219 int srcStride[], int srcSliceY, int srcSliceH,
00220 uint8_t *dstParam[], int dstStride[])
00221 {
00222 uint8_t *dst = dstParam[0] + dstStride[0] * srcSliceY;
00223
00224 yv12touyvy(src[0], src[1], src[2], dst, c->srcW, srcSliceH, srcStride[0],
00225 srcStride[1], dstStride[0]);
00226
00227 return srcSliceH;
00228 }
00229
00230 static int yuv422pToYuy2Wrapper(SwsContext *c, const uint8_t *src[],
00231 int srcStride[], int srcSliceY, int srcSliceH,
00232 uint8_t *dstParam[], int dstStride[])
00233 {
00234 uint8_t *dst = dstParam[0] + dstStride[0] * srcSliceY;
00235
00236 yuv422ptoyuy2(src[0], src[1], src[2], dst, c->srcW, srcSliceH, srcStride[0],
00237 srcStride[1], dstStride[0]);
00238
00239 return srcSliceH;
00240 }
00241
00242 static int yuv422pToUyvyWrapper(SwsContext *c, const uint8_t *src[],
00243 int srcStride[], int srcSliceY, int srcSliceH,
00244 uint8_t *dstParam[], int dstStride[])
00245 {
00246 uint8_t *dst = dstParam[0] + dstStride[0] * srcSliceY;
00247
00248 yuv422ptouyvy(src[0], src[1], src[2], dst, c->srcW, srcSliceH, srcStride[0],
00249 srcStride[1], dstStride[0]);
00250
00251 return srcSliceH;
00252 }
00253
00254 static int yuyvToYuv420Wrapper(SwsContext *c, const uint8_t *src[],
00255 int srcStride[], int srcSliceY, int srcSliceH,
00256 uint8_t *dstParam[], int dstStride[])
00257 {
00258 uint8_t *ydst = dstParam[0] + dstStride[0] * srcSliceY;
00259 uint8_t *udst = dstParam[1] + dstStride[1] * srcSliceY / 2;
00260 uint8_t *vdst = dstParam[2] + dstStride[2] * srcSliceY / 2;
00261
00262 yuyvtoyuv420(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0],
00263 dstStride[1], srcStride[0]);
00264
00265 if (dstParam[3])
00266 fillPlane(dstParam[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
00267
00268 return srcSliceH;
00269 }
00270
00271 static int yuyvToYuv422Wrapper(SwsContext *c, const uint8_t *src[],
00272 int srcStride[], int srcSliceY, int srcSliceH,
00273 uint8_t *dstParam[], int dstStride[])
00274 {
00275 uint8_t *ydst = dstParam[0] + dstStride[0] * srcSliceY;
00276 uint8_t *udst = dstParam[1] + dstStride[1] * srcSliceY;
00277 uint8_t *vdst = dstParam[2] + dstStride[2] * srcSliceY;
00278
00279 yuyvtoyuv422(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0],
00280 dstStride[1], srcStride[0]);
00281
00282 return srcSliceH;
00283 }
00284
00285 static int uyvyToYuv420Wrapper(SwsContext *c, const uint8_t *src[],
00286 int srcStride[], int srcSliceY, int srcSliceH,
00287 uint8_t *dstParam[], int dstStride[])
00288 {
00289 uint8_t *ydst = dstParam[0] + dstStride[0] * srcSliceY;
00290 uint8_t *udst = dstParam[1] + dstStride[1] * srcSliceY / 2;
00291 uint8_t *vdst = dstParam[2] + dstStride[2] * srcSliceY / 2;
00292
00293 uyvytoyuv420(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0],
00294 dstStride[1], srcStride[0]);
00295
00296 if (dstParam[3])
00297 fillPlane(dstParam[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
00298
00299 return srcSliceH;
00300 }
00301
00302 static int uyvyToYuv422Wrapper(SwsContext *c, const uint8_t *src[],
00303 int srcStride[], int srcSliceY, int srcSliceH,
00304 uint8_t *dstParam[], int dstStride[])
00305 {
00306 uint8_t *ydst = dstParam[0] + dstStride[0] * srcSliceY;
00307 uint8_t *udst = dstParam[1] + dstStride[1] * srcSliceY;
00308 uint8_t *vdst = dstParam[2] + dstStride[2] * srcSliceY;
00309
00310 uyvytoyuv422(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0],
00311 dstStride[1], srcStride[0]);
00312
00313 return srcSliceH;
00314 }
00315
00316 static void gray8aToPacked32(const uint8_t *src, uint8_t *dst, int num_pixels,
00317 const uint8_t *palette)
00318 {
00319 int i;
00320 for (i = 0; i < num_pixels; i++)
00321 ((uint32_t *) dst)[i] = ((const uint32_t *) palette)[src[i << 1]] | (src[(i << 1) + 1] << 24);
00322 }
00323
00324 static void gray8aToPacked32_1(const uint8_t *src, uint8_t *dst, int num_pixels,
00325 const uint8_t *palette)
00326 {
00327 int i;
00328
00329 for (i = 0; i < num_pixels; i++)
00330 ((uint32_t *) dst)[i] = ((const uint32_t *) palette)[src[i << 1]] | src[(i << 1) + 1];
00331 }
00332
00333 static void gray8aToPacked24(const uint8_t *src, uint8_t *dst, int num_pixels,
00334 const uint8_t *palette)
00335 {
00336 int i;
00337
00338 for (i = 0; i < num_pixels; i++) {
00339
00340 dst[0] = palette[src[i << 1] * 4 + 0];
00341 dst[1] = palette[src[i << 1] * 4 + 1];
00342 dst[2] = palette[src[i << 1] * 4 + 2];
00343 dst += 3;
00344 }
00345 }
00346
00347 static int packed_16bpc_bswap(SwsContext *c, const uint8_t *src[],
00348 int srcStride[], int srcSliceY, int srcSliceH,
00349 uint8_t *dst[], int dstStride[])
00350 {
00351 int i, j;
00352 int srcstr = srcStride[0] >> 1;
00353 int dststr = dstStride[0] >> 1;
00354 uint16_t *dstPtr = (uint16_t *) dst[0];
00355 const uint16_t *srcPtr = (const uint16_t *) src[0];
00356 int min_stride = FFMIN(srcstr, dststr);
00357
00358 for (i = 0; i < srcSliceH; i++) {
00359 for (j = 0; j < min_stride; j++) {
00360 dstPtr[j] = av_bswap16(srcPtr[j]);
00361 }
00362 srcPtr += srcstr;
00363 dstPtr += dststr;
00364 }
00365
00366 return srcSliceH;
00367 }
00368
00369 static int palToRgbWrapper(SwsContext *c, const uint8_t *src[], int srcStride[],
00370 int srcSliceY, int srcSliceH, uint8_t *dst[],
00371 int dstStride[])
00372 {
00373 const enum PixelFormat srcFormat = c->srcFormat;
00374 const enum PixelFormat dstFormat = c->dstFormat;
00375 void (*conv)(const uint8_t *src, uint8_t *dst, int num_pixels,
00376 const uint8_t *palette) = NULL;
00377 int i;
00378 uint8_t *dstPtr = dst[0] + dstStride[0] * srcSliceY;
00379 const uint8_t *srcPtr = src[0];
00380
00381 if (srcFormat == PIX_FMT_GRAY8A) {
00382 switch (dstFormat) {
00383 case PIX_FMT_RGB32 : conv = gray8aToPacked32; break;
00384 case PIX_FMT_BGR32 : conv = gray8aToPacked32; break;
00385 case PIX_FMT_BGR32_1: conv = gray8aToPacked32_1; break;
00386 case PIX_FMT_RGB32_1: conv = gray8aToPacked32_1; break;
00387 case PIX_FMT_RGB24 : conv = gray8aToPacked24; break;
00388 case PIX_FMT_BGR24 : conv = gray8aToPacked24; break;
00389 }
00390 } else if (usePal(srcFormat)) {
00391 switch (dstFormat) {
00392 case PIX_FMT_RGB32 : conv = sws_convertPalette8ToPacked32; break;
00393 case PIX_FMT_BGR32 : conv = sws_convertPalette8ToPacked32; break;
00394 case PIX_FMT_BGR32_1: conv = sws_convertPalette8ToPacked32; break;
00395 case PIX_FMT_RGB32_1: conv = sws_convertPalette8ToPacked32; break;
00396 case PIX_FMT_RGB24 : conv = sws_convertPalette8ToPacked24; break;
00397 case PIX_FMT_BGR24 : conv = sws_convertPalette8ToPacked24; break;
00398 }
00399 }
00400
00401 if (!conv)
00402 av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
00403 av_get_pix_fmt_name(srcFormat), av_get_pix_fmt_name(dstFormat));
00404 else {
00405 for (i = 0; i < srcSliceH; i++) {
00406 conv(srcPtr, dstPtr, c->srcW, (uint8_t *) c->pal_rgb);
00407 srcPtr += srcStride[0];
00408 dstPtr += dstStride[0];
00409 }
00410 }
00411
00412 return srcSliceH;
00413 }
00414
00415 static void gbr24ptopacked24(const uint8_t *src[], int srcStride[],
00416 uint8_t *dst, int dstStride, int srcSliceH,
00417 int width)
00418 {
00419 int x, h, i;
00420 for (h = 0; h < srcSliceH; h++) {
00421 uint8_t *dest = dst + dstStride * h;
00422 for (x = 0; x < width; x++) {
00423 *dest++ = src[0][x];
00424 *dest++ = src[1][x];
00425 *dest++ = src[2][x];
00426 }
00427
00428 for (i = 0; i < 3; i++)
00429 src[i] += srcStride[i];
00430 }
00431 }
00432
00433 static void gbr24ptopacked32(const uint8_t *src[], int srcStride[],
00434 uint8_t *dst, int dstStride, int srcSliceH,
00435 int alpha_first, int width)
00436 {
00437 int x, h, i;
00438 for (h = 0; h < srcSliceH; h++) {
00439 uint8_t *dest = dst + dstStride * h;
00440
00441 if (alpha_first) {
00442 for (x = 0; x < width; x++) {
00443 *dest++ = 0xff;
00444 *dest++ = src[0][x];
00445 *dest++ = src[1][x];
00446 *dest++ = src[2][x];
00447 }
00448 } else {
00449 for (x = 0; x < width; x++) {
00450 *dest++ = src[0][x];
00451 *dest++ = src[1][x];
00452 *dest++ = src[2][x];
00453 *dest++ = 0xff;
00454 }
00455 }
00456
00457 for (i = 0; i < 3; i++)
00458 src[i] += srcStride[i];
00459 }
00460 }
00461
00462 static int planarRgbToRgbWrapper(SwsContext *c, const uint8_t *src[],
00463 int srcStride[], int srcSliceY, int srcSliceH,
00464 uint8_t *dst[], int dstStride[])
00465 {
00466 int alpha_first = 0;
00467 if (c->srcFormat != PIX_FMT_GBRP) {
00468 av_log(c, AV_LOG_ERROR, "unsupported planar RGB conversion %s -> %s\n",
00469 av_get_pix_fmt_name(c->srcFormat),
00470 av_get_pix_fmt_name(c->dstFormat));
00471 return srcSliceH;
00472 }
00473
00474 switch (c->dstFormat) {
00475 case PIX_FMT_BGR24:
00476 gbr24ptopacked24((const uint8_t *[]) { src[1], src[0], src[2] },
00477 (int []) { srcStride[1], srcStride[0], srcStride[2] },
00478 dst[0] + srcSliceY * dstStride[0], dstStride[0],
00479 srcSliceH, c->srcW);
00480 break;
00481
00482 case PIX_FMT_RGB24:
00483 gbr24ptopacked24((const uint8_t *[]) { src[2], src[0], src[1] },
00484 (int []) { srcStride[2], srcStride[0], srcStride[1] },
00485 dst[0] + srcSliceY * dstStride[0], dstStride[0],
00486 srcSliceH, c->srcW);
00487 break;
00488
00489 case PIX_FMT_ARGB:
00490 alpha_first = 1;
00491 case PIX_FMT_RGBA:
00492 gbr24ptopacked32((const uint8_t *[]) { src[2], src[0], src[1] },
00493 (int []) { srcStride[2], srcStride[0], srcStride[1] },
00494 dst[0] + srcSliceY * dstStride[0], dstStride[0],
00495 srcSliceH, alpha_first, c->srcW);
00496 break;
00497
00498 case PIX_FMT_ABGR:
00499 alpha_first = 1;
00500 case PIX_FMT_BGRA:
00501 gbr24ptopacked32((const uint8_t *[]) { src[1], src[0], src[2] },
00502 (int []) { srcStride[1], srcStride[0], srcStride[2] },
00503 dst[0] + srcSliceY * dstStride[0], dstStride[0],
00504 srcSliceH, alpha_first, c->srcW);
00505 break;
00506
00507 default:
00508 av_log(c, AV_LOG_ERROR,
00509 "unsupported planar RGB conversion %s -> %s\n",
00510 av_get_pix_fmt_name(c->srcFormat),
00511 av_get_pix_fmt_name(c->dstFormat));
00512 }
00513
00514 return srcSliceH;
00515 }
00516
00517 #define isRGBA32(x) ( \
00518 (x) == PIX_FMT_ARGB \
00519 || (x) == PIX_FMT_RGBA \
00520 || (x) == PIX_FMT_BGRA \
00521 || (x) == PIX_FMT_ABGR \
00522 )
00523
00524
00525 typedef void (* rgbConvFn) (const uint8_t *, uint8_t *, int);
00526 static rgbConvFn findRgbConvFn(SwsContext *c)
00527 {
00528 const enum PixelFormat srcFormat = c->srcFormat;
00529 const enum PixelFormat dstFormat = c->dstFormat;
00530 const int srcId = c->srcFormatBpp;
00531 const int dstId = c->dstFormatBpp;
00532 rgbConvFn conv = NULL;
00533
00534 #define IS_NOT_NE(bpp, fmt) \
00535 (((bpp + 7) >> 3) == 2 && \
00536 (!(av_pix_fmt_descriptors[fmt].flags & PIX_FMT_BE) != !HAVE_BIGENDIAN))
00537
00538
00539 if (IS_NOT_NE(srcId, srcFormat) || IS_NOT_NE(dstId, dstFormat))
00540 return NULL;
00541
00542 #define CONV_IS(src, dst) (srcFormat == PIX_FMT_##src && dstFormat == PIX_FMT_##dst)
00543
00544 if (isRGBA32(srcFormat) && isRGBA32(dstFormat)) {
00545 if ( CONV_IS(ABGR, RGBA)
00546 || CONV_IS(ARGB, BGRA)
00547 || CONV_IS(BGRA, ARGB)
00548 || CONV_IS(RGBA, ABGR)) conv = shuffle_bytes_3210;
00549 else if (CONV_IS(ABGR, ARGB)
00550 || CONV_IS(ARGB, ABGR)) conv = shuffle_bytes_0321;
00551 else if (CONV_IS(ABGR, BGRA)
00552 || CONV_IS(ARGB, RGBA)) conv = shuffle_bytes_1230;
00553 else if (CONV_IS(BGRA, RGBA)
00554 || CONV_IS(RGBA, BGRA)) conv = shuffle_bytes_2103;
00555 else if (CONV_IS(BGRA, ABGR)
00556 || CONV_IS(RGBA, ARGB)) conv = shuffle_bytes_3012;
00557 } else
00558
00559 if ((isBGRinInt(srcFormat) && isBGRinInt(dstFormat)) ||
00560 (isRGBinInt(srcFormat) && isRGBinInt(dstFormat))) {
00561 switch (srcId | (dstId << 16)) {
00562 case 0x000F000C: conv = rgb12to15; break;
00563 case 0x000F0010: conv = rgb16to15; break;
00564 case 0x000F0018: conv = rgb24to15; break;
00565 case 0x000F0020: conv = rgb32to15; break;
00566 case 0x0010000F: conv = rgb15to16; break;
00567 case 0x00100018: conv = rgb24to16; break;
00568 case 0x00100020: conv = rgb32to16; break;
00569 case 0x0018000F: conv = rgb15to24; break;
00570 case 0x00180010: conv = rgb16to24; break;
00571 case 0x00180020: conv = rgb32to24; break;
00572 case 0x0020000F: conv = rgb15to32; break;
00573 case 0x00200010: conv = rgb16to32; break;
00574 case 0x00200018: conv = rgb24to32; break;
00575 }
00576 } else if ((isBGRinInt(srcFormat) && isRGBinInt(dstFormat)) ||
00577 (isRGBinInt(srcFormat) && isBGRinInt(dstFormat))) {
00578 switch (srcId | (dstId << 16)) {
00579 case 0x000C000C: conv = rgb12tobgr12; break;
00580 case 0x000F000F: conv = rgb15tobgr15; break;
00581 case 0x000F0010: conv = rgb16tobgr15; break;
00582 case 0x000F0018: conv = rgb24tobgr15; break;
00583 case 0x000F0020: conv = rgb32tobgr15; break;
00584 case 0x0010000F: conv = rgb15tobgr16; break;
00585 case 0x00100010: conv = rgb16tobgr16; break;
00586 case 0x00100018: conv = rgb24tobgr16; break;
00587 case 0x00100020: conv = rgb32tobgr16; break;
00588 case 0x0018000F: conv = rgb15tobgr24; break;
00589 case 0x00180010: conv = rgb16tobgr24; break;
00590 case 0x00180018: conv = rgb24tobgr24; break;
00591 case 0x00180020: conv = rgb32tobgr24; break;
00592 case 0x0020000F: conv = rgb15tobgr32; break;
00593 case 0x00200010: conv = rgb16tobgr32; break;
00594 case 0x00200018: conv = rgb24tobgr32; break;
00595 }
00596 }
00597
00598 return conv;
00599 }
00600
00601
00602 static int rgbToRgbWrapper(SwsContext *c, const uint8_t *src[], int srcStride[],
00603 int srcSliceY, int srcSliceH, uint8_t *dst[],
00604 int dstStride[])
00605
00606 {
00607 const enum PixelFormat srcFormat = c->srcFormat;
00608 const enum PixelFormat dstFormat = c->dstFormat;
00609 const int srcBpp = (c->srcFormatBpp + 7) >> 3;
00610 const int dstBpp = (c->dstFormatBpp + 7) >> 3;
00611 rgbConvFn conv = findRgbConvFn(c);
00612
00613 if (!conv) {
00614 av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
00615 av_get_pix_fmt_name(srcFormat), av_get_pix_fmt_name(dstFormat));
00616 } else {
00617 const uint8_t *srcPtr = src[0];
00618 uint8_t *dstPtr = dst[0];
00619 if ((srcFormat == PIX_FMT_RGB32_1 || srcFormat == PIX_FMT_BGR32_1) &&
00620 !isRGBA32(dstFormat))
00621 srcPtr += ALT32_CORR;
00622
00623 if ((dstFormat == PIX_FMT_RGB32_1 || dstFormat == PIX_FMT_BGR32_1) &&
00624 !isRGBA32(srcFormat))
00625 dstPtr += ALT32_CORR;
00626
00627 if (dstStride[0] * srcBpp == srcStride[0] * dstBpp && srcStride[0] > 0 &&
00628 !(srcStride[0] % srcBpp))
00629 conv(srcPtr, dstPtr + dstStride[0] * srcSliceY,
00630 srcSliceH * srcStride[0]);
00631 else {
00632 int i;
00633 dstPtr += dstStride[0] * srcSliceY;
00634
00635 for (i = 0; i < srcSliceH; i++) {
00636 conv(srcPtr, dstPtr, c->srcW * srcBpp);
00637 srcPtr += srcStride[0];
00638 dstPtr += dstStride[0];
00639 }
00640 }
00641 }
00642 return srcSliceH;
00643 }
00644
00645 static int bgr24ToYv12Wrapper(SwsContext *c, const uint8_t *src[],
00646 int srcStride[], int srcSliceY, int srcSliceH,
00647 uint8_t *dst[], int dstStride[])
00648 {
00649 rgb24toyv12(
00650 src[0],
00651 dst[0] + srcSliceY * dstStride[0],
00652 dst[1] + (srcSliceY >> 1) * dstStride[1],
00653 dst[2] + (srcSliceY >> 1) * dstStride[2],
00654 c->srcW, srcSliceH,
00655 dstStride[0], dstStride[1], srcStride[0]);
00656 if (dst[3])
00657 fillPlane(dst[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
00658 return srcSliceH;
00659 }
00660
00661 static int yvu9ToYv12Wrapper(SwsContext *c, const uint8_t *src[],
00662 int srcStride[], int srcSliceY, int srcSliceH,
00663 uint8_t *dst[], int dstStride[])
00664 {
00665 copyPlane(src[0], srcStride[0], srcSliceY, srcSliceH, c->srcW,
00666 dst[0], dstStride[0]);
00667
00668 planar2x(src[1], dst[1] + dstStride[1] * (srcSliceY >> 1), c->chrSrcW,
00669 srcSliceH >> 2, srcStride[1], dstStride[1]);
00670 planar2x(src[2], dst[2] + dstStride[2] * (srcSliceY >> 1), c->chrSrcW,
00671 srcSliceH >> 2, srcStride[2], dstStride[2]);
00672 if (dst[3])
00673 fillPlane(dst[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
00674 return srcSliceH;
00675 }
00676
00677
00678 static int packedCopyWrapper(SwsContext *c, const uint8_t *src[],
00679 int srcStride[], int srcSliceY, int srcSliceH,
00680 uint8_t *dst[], int dstStride[])
00681 {
00682 if (dstStride[0] == srcStride[0] && srcStride[0] > 0)
00683 memcpy(dst[0] + dstStride[0] * srcSliceY, src[0], srcSliceH * dstStride[0]);
00684 else {
00685 int i;
00686 const uint8_t *srcPtr = src[0];
00687 uint8_t *dstPtr = dst[0] + dstStride[0] * srcSliceY;
00688 int length = 0;
00689
00690
00691 while (length + c->srcW <= FFABS(dstStride[0]) &&
00692 length + c->srcW <= FFABS(srcStride[0]))
00693 length += c->srcW;
00694 assert(length != 0);
00695
00696 for (i = 0; i < srcSliceH; i++) {
00697 memcpy(dstPtr, srcPtr, length);
00698 srcPtr += srcStride[0];
00699 dstPtr += dstStride[0];
00700 }
00701 }
00702 return srcSliceH;
00703 }
00704
00705 #define DITHER_COPY(dst, dstStride, src, srcStride, bswap, dbswap)\
00706 uint16_t scale= dither_scale[dst_depth-1][src_depth-1];\
00707 int shift= src_depth-dst_depth + dither_scale[src_depth-2][dst_depth-1];\
00708 for (i = 0; i < height; i++) {\
00709 const uint8_t *dither= dithers[src_depth-9][i&7];\
00710 for (j = 0; j < length-7; j+=8){\
00711 dst[j+0] = dbswap((bswap(src[j+0]) + dither[0])*scale>>shift);\
00712 dst[j+1] = dbswap((bswap(src[j+1]) + dither[1])*scale>>shift);\
00713 dst[j+2] = dbswap((bswap(src[j+2]) + dither[2])*scale>>shift);\
00714 dst[j+3] = dbswap((bswap(src[j+3]) + dither[3])*scale>>shift);\
00715 dst[j+4] = dbswap((bswap(src[j+4]) + dither[4])*scale>>shift);\
00716 dst[j+5] = dbswap((bswap(src[j+5]) + dither[5])*scale>>shift);\
00717 dst[j+6] = dbswap((bswap(src[j+6]) + dither[6])*scale>>shift);\
00718 dst[j+7] = dbswap((bswap(src[j+7]) + dither[7])*scale>>shift);\
00719 }\
00720 for (; j < length; j++)\
00721 dst[j] = dbswap((bswap(src[j]) + dither[j&7])*scale>>shift);\
00722 dst += dstStride;\
00723 src += srcStride;\
00724 }
00725
00726 static int planarCopyWrapper(SwsContext *c, const uint8_t *src[],
00727 int srcStride[], int srcSliceY, int srcSliceH,
00728 uint8_t *dst[], int dstStride[])
00729 {
00730 int plane, i, j;
00731 for (plane = 0; plane < 4; plane++) {
00732 int length = (plane == 0 || plane == 3) ? c->srcW : -((-c->srcW ) >> c->chrDstHSubSample);
00733 int y = (plane == 0 || plane == 3) ? srcSliceY: -((-srcSliceY) >> c->chrDstVSubSample);
00734 int height = (plane == 0 || plane == 3) ? srcSliceH: -((-srcSliceH) >> c->chrDstVSubSample);
00735 const uint8_t *srcPtr = src[plane];
00736 uint8_t *dstPtr = dst[plane] + dstStride[plane] * y;
00737 int shiftonly= plane==1 || plane==2 || (!c->srcRange && plane==0);
00738
00739 if (!dst[plane])
00740 continue;
00741
00742 if (plane == 1 && !dst[2]) continue;
00743 if (!src[plane] || (plane == 1 && !src[2])) {
00744 if (is16BPS(c->dstFormat) || isNBPS(c->dstFormat)) {
00745 fillPlane16(dst[plane], dstStride[plane], length, height, y,
00746 plane == 3, av_pix_fmt_descriptors[c->dstFormat].comp[plane].depth_minus1);
00747 } else {
00748 fillPlane(dst[plane], dstStride[plane], length, height, y,
00749 (plane == 3) ? 255 : 128);
00750 }
00751 } else {
00752 if(isNBPS(c->srcFormat) || isNBPS(c->dstFormat)
00753 || (is16BPS(c->srcFormat) != is16BPS(c->dstFormat))
00754 ) {
00755 const int src_depth = av_pix_fmt_descriptors[c->srcFormat].comp[plane].depth_minus1 + 1;
00756 const int dst_depth = av_pix_fmt_descriptors[c->dstFormat].comp[plane].depth_minus1 + 1;
00757 const uint16_t *srcPtr2 = (const uint16_t *) srcPtr;
00758 uint16_t *dstPtr2 = (uint16_t*)dstPtr;
00759
00760 if (dst_depth == 8) {
00761 if(isBE(c->srcFormat) == HAVE_BIGENDIAN){
00762 DITHER_COPY(dstPtr, dstStride[plane], srcPtr2, srcStride[plane]/2, , )
00763 } else {
00764 DITHER_COPY(dstPtr, dstStride[plane], srcPtr2, srcStride[plane]/2, av_bswap16, )
00765 }
00766 } else if (src_depth == 8) {
00767 for (i = 0; i < height; i++) {
00768 #define COPY816(w)\
00769 if(shiftonly){\
00770 for (j = 0; j < length; j++)\
00771 w(&dstPtr2[j], srcPtr[j]<<(dst_depth-8));\
00772 }else{\
00773 for (j = 0; j < length; j++)\
00774 w(&dstPtr2[j], (srcPtr[j]<<(dst_depth-8)) |\
00775 (srcPtr[j]>>(2*8-dst_depth)));\
00776 }
00777 if(isBE(c->dstFormat)){
00778 COPY816(AV_WB16)
00779 } else {
00780 COPY816(AV_WL16)
00781 }
00782 dstPtr2 += dstStride[plane]/2;
00783 srcPtr += srcStride[plane];
00784 }
00785 } else if (src_depth <= dst_depth) {
00786 for (i = 0; i < height; i++) {
00787 #define COPY_UP(r,w) \
00788 if(shiftonly){\
00789 for (j = 0; j < length; j++){ \
00790 unsigned int v= r(&srcPtr2[j]);\
00791 w(&dstPtr2[j], v<<(dst_depth-src_depth));\
00792 }\
00793 }else{\
00794 for (j = 0; j < length; j++){ \
00795 unsigned int v= r(&srcPtr2[j]);\
00796 w(&dstPtr2[j], (v<<(dst_depth-src_depth)) | \
00797 (v>>(2*src_depth-dst_depth)));\
00798 }\
00799 }
00800 if(isBE(c->srcFormat)){
00801 if(isBE(c->dstFormat)){
00802 COPY_UP(AV_RB16, AV_WB16)
00803 } else {
00804 COPY_UP(AV_RB16, AV_WL16)
00805 }
00806 } else {
00807 if(isBE(c->dstFormat)){
00808 COPY_UP(AV_RL16, AV_WB16)
00809 } else {
00810 COPY_UP(AV_RL16, AV_WL16)
00811 }
00812 }
00813 dstPtr2 += dstStride[plane]/2;
00814 srcPtr2 += srcStride[plane]/2;
00815 }
00816 } else {
00817 if(isBE(c->srcFormat) == HAVE_BIGENDIAN){
00818 if(isBE(c->dstFormat) == HAVE_BIGENDIAN){
00819 DITHER_COPY(dstPtr2, dstStride[plane]/2, srcPtr2, srcStride[plane]/2, , )
00820 } else {
00821 DITHER_COPY(dstPtr2, dstStride[plane]/2, srcPtr2, srcStride[plane]/2, , av_bswap16)
00822 }
00823 }else{
00824 if(isBE(c->dstFormat) == HAVE_BIGENDIAN){
00825 DITHER_COPY(dstPtr2, dstStride[plane]/2, srcPtr2, srcStride[plane]/2, av_bswap16, )
00826 } else {
00827 DITHER_COPY(dstPtr2, dstStride[plane]/2, srcPtr2, srcStride[plane]/2, av_bswap16, av_bswap16)
00828 }
00829 }
00830 }
00831 } else if (is16BPS(c->srcFormat) && is16BPS(c->dstFormat) &&
00832 isBE(c->srcFormat) != isBE(c->dstFormat)) {
00833
00834 for (i = 0; i < height; i++) {
00835 for (j = 0; j < length; j++)
00836 ((uint16_t *) dstPtr)[j] = av_bswap16(((const uint16_t *) srcPtr)[j]);
00837 srcPtr += srcStride[plane];
00838 dstPtr += dstStride[plane];
00839 }
00840 } else if (dstStride[plane] == srcStride[plane] &&
00841 srcStride[plane] > 0 && srcStride[plane] == length) {
00842 memcpy(dst[plane] + dstStride[plane] * y, src[plane],
00843 height * dstStride[plane]);
00844 } else {
00845 if (is16BPS(c->srcFormat) && is16BPS(c->dstFormat))
00846 length *= 2;
00847 else if (!av_pix_fmt_descriptors[c->srcFormat].comp[0].depth_minus1)
00848 length >>= 3;
00849 for (i = 0; i < height; i++) {
00850 memcpy(dstPtr, srcPtr, length);
00851 srcPtr += srcStride[plane];
00852 dstPtr += dstStride[plane];
00853 }
00854 }
00855 }
00856 }
00857 return srcSliceH;
00858 }
00859
00860
00861 #define IS_DIFFERENT_ENDIANESS(src_fmt, dst_fmt, pix_fmt) \
00862 ((src_fmt == pix_fmt ## BE && dst_fmt == pix_fmt ## LE) || \
00863 (src_fmt == pix_fmt ## LE && dst_fmt == pix_fmt ## BE))
00864
00865
00866 void ff_get_unscaled_swscale(SwsContext *c)
00867 {
00868 const enum PixelFormat srcFormat = c->srcFormat;
00869 const enum PixelFormat dstFormat = c->dstFormat;
00870 const int flags = c->flags;
00871 const int dstH = c->dstH;
00872 int needsDither;
00873
00874 needsDither = isAnyRGB(dstFormat) &&
00875 c->dstFormatBpp < 24 &&
00876 (c->dstFormatBpp < c->srcFormatBpp || (!isAnyRGB(srcFormat)));
00877
00878
00879 if ((srcFormat == PIX_FMT_YUV420P || srcFormat == PIX_FMT_YUVA420P) &&
00880 (dstFormat == PIX_FMT_NV12 || dstFormat == PIX_FMT_NV21)) {
00881 c->swScale = planarToNv12Wrapper;
00882 }
00883
00884 if ((srcFormat == PIX_FMT_YUV420P || srcFormat == PIX_FMT_YUV422P ||
00885 srcFormat == PIX_FMT_YUVA420P) && isAnyRGB(dstFormat) &&
00886 !(flags & SWS_ACCURATE_RND) && !(dstH & 1)) {
00887 c->swScale = ff_yuv2rgb_get_func_ptr(c);
00888 }
00889
00890 if (srcFormat == PIX_FMT_YUV410P &&
00891 (dstFormat == PIX_FMT_YUV420P || dstFormat == PIX_FMT_YUVA420P) &&
00892 !(flags & SWS_BITEXACT)) {
00893 c->swScale = yvu9ToYv12Wrapper;
00894 }
00895
00896
00897 if (srcFormat == PIX_FMT_BGR24 &&
00898 (dstFormat == PIX_FMT_YUV420P || dstFormat == PIX_FMT_YUVA420P) &&
00899 !(flags & SWS_ACCURATE_RND))
00900 c->swScale = bgr24ToYv12Wrapper;
00901
00902
00903 if (isAnyRGB(srcFormat) && isAnyRGB(dstFormat) && findRgbConvFn(c)
00904 && (!needsDither || (c->flags&(SWS_FAST_BILINEAR|SWS_POINT))))
00905 c->swScale= rgbToRgbWrapper;
00906
00907 #define isByteRGB(f) (\
00908 f == PIX_FMT_RGB32 ||\
00909 f == PIX_FMT_RGB32_1 ||\
00910 f == PIX_FMT_RGB24 ||\
00911 f == PIX_FMT_BGR32 ||\
00912 f == PIX_FMT_BGR32_1 ||\
00913 f == PIX_FMT_BGR24)
00914
00915 if (isAnyRGB(srcFormat) && isPlanar(srcFormat) && isByteRGB(dstFormat))
00916 c->swScale = planarRgbToRgbWrapper;
00917
00918
00919 if (IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, PIX_FMT_BGR444) ||
00920 IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, PIX_FMT_BGR48) ||
00921 IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, PIX_FMT_BGR555) ||
00922 IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, PIX_FMT_BGR565) ||
00923 IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, PIX_FMT_GRAY16) ||
00924 IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, PIX_FMT_RGB444) ||
00925 IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, PIX_FMT_RGB48) ||
00926 IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, PIX_FMT_RGB555) ||
00927 IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, PIX_FMT_RGB565))
00928 c->swScale = packed_16bpc_bswap;
00929
00930 if (usePal(srcFormat) && isByteRGB(dstFormat))
00931 c->swScale = palToRgbWrapper;
00932
00933 if (srcFormat == PIX_FMT_YUV422P) {
00934 if (dstFormat == PIX_FMT_YUYV422)
00935 c->swScale = yuv422pToYuy2Wrapper;
00936 else if (dstFormat == PIX_FMT_UYVY422)
00937 c->swScale = yuv422pToUyvyWrapper;
00938 }
00939
00940
00941 if (c->flags&(SWS_FAST_BILINEAR|SWS_POINT)) {
00942
00943 if (srcFormat == PIX_FMT_YUV420P || srcFormat == PIX_FMT_YUVA420P) {
00944 if (dstFormat == PIX_FMT_YUYV422)
00945 c->swScale = planarToYuy2Wrapper;
00946 else if (dstFormat == PIX_FMT_UYVY422)
00947 c->swScale = planarToUyvyWrapper;
00948 }
00949 }
00950 if (srcFormat == PIX_FMT_YUYV422 &&
00951 (dstFormat == PIX_FMT_YUV420P || dstFormat == PIX_FMT_YUVA420P))
00952 c->swScale = yuyvToYuv420Wrapper;
00953 if (srcFormat == PIX_FMT_UYVY422 &&
00954 (dstFormat == PIX_FMT_YUV420P || dstFormat == PIX_FMT_YUVA420P))
00955 c->swScale = uyvyToYuv420Wrapper;
00956 if (srcFormat == PIX_FMT_YUYV422 && dstFormat == PIX_FMT_YUV422P)
00957 c->swScale = yuyvToYuv422Wrapper;
00958 if (srcFormat == PIX_FMT_UYVY422 && dstFormat == PIX_FMT_YUV422P)
00959 c->swScale = uyvyToYuv422Wrapper;
00960
00961 #define isPlanarGray(x) (isGray(x) && (x) != PIX_FMT_GRAY8A)
00962
00963 if ( srcFormat == dstFormat ||
00964 (srcFormat == PIX_FMT_YUVA420P && dstFormat == PIX_FMT_YUV420P) ||
00965 (srcFormat == PIX_FMT_YUV420P && dstFormat == PIX_FMT_YUVA420P) ||
00966 (isPlanarYUV(srcFormat) && isPlanarGray(dstFormat)) ||
00967 (isPlanarYUV(dstFormat) && isPlanarGray(srcFormat)) ||
00968 (isPlanarGray(dstFormat) && isPlanarGray(srcFormat)) ||
00969 (isPlanarYUV(srcFormat) && isPlanarYUV(dstFormat) &&
00970 c->chrDstHSubSample == c->chrSrcHSubSample &&
00971 c->chrDstVSubSample == c->chrSrcVSubSample &&
00972 dstFormat != PIX_FMT_NV12 && dstFormat != PIX_FMT_NV21 &&
00973 srcFormat != PIX_FMT_NV12 && srcFormat != PIX_FMT_NV21))
00974 {
00975 if (isPacked(c->srcFormat))
00976 c->swScale = packedCopyWrapper;
00977 else
00978 c->swScale = planarCopyWrapper;
00979 }
00980
00981 if (ARCH_BFIN)
00982 ff_bfin_get_unscaled_swscale(c);
00983 if (HAVE_ALTIVEC)
00984 ff_swscale_get_unscaled_altivec(c);
00985 }
00986
00987 static void reset_ptr(const uint8_t *src[], int format)
00988 {
00989 if (!isALPHA(format))
00990 src[3] = NULL;
00991 if (!isPlanar(format)) {
00992 src[3] = src[2] = NULL;
00993
00994 if (!usePal(format))
00995 src[1] = NULL;
00996 }
00997 }
00998
00999 static int check_image_pointers(const uint8_t * const data[4], enum PixelFormat pix_fmt,
01000 const int linesizes[4])
01001 {
01002 const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
01003 int i;
01004
01005 for (i = 0; i < 4; i++) {
01006 int plane = desc->comp[i].plane;
01007 if (!data[plane] || !linesizes[plane])
01008 return 0;
01009 }
01010
01011 return 1;
01012 }
01013
01018 int attribute_align_arg sws_scale(struct SwsContext *c,
01019 const uint8_t * const srcSlice[],
01020 const int srcStride[], int srcSliceY,
01021 int srcSliceH, uint8_t *const dst[],
01022 const int dstStride[])
01023 {
01024 int i, ret;
01025 const uint8_t *src2[4] = { srcSlice[0], srcSlice[1], srcSlice[2], srcSlice[3] };
01026 uint8_t *dst2[4] = { dst[0], dst[1], dst[2], dst[3] };
01027 uint8_t *rgb0_tmp = NULL;
01028
01029
01030 if (srcSliceH == 0)
01031 return 0;
01032
01033 if (!check_image_pointers(srcSlice, c->srcFormat, srcStride)) {
01034 av_log(c, AV_LOG_ERROR, "bad src image pointers\n");
01035 return 0;
01036 }
01037 if (!check_image_pointers((const uint8_t* const*)dst, c->dstFormat, dstStride)) {
01038 av_log(c, AV_LOG_ERROR, "bad dst image pointers\n");
01039 return 0;
01040 }
01041
01042 if (c->sliceDir == 0 && srcSliceY != 0 && srcSliceY + srcSliceH != c->srcH) {
01043 av_log(c, AV_LOG_ERROR, "Slices start in the middle!\n");
01044 return 0;
01045 }
01046 if (c->sliceDir == 0) {
01047 if (srcSliceY == 0) c->sliceDir = 1; else c->sliceDir = -1;
01048 }
01049
01050 if (usePal(c->srcFormat)) {
01051 for (i = 0; i < 256; i++) {
01052 int p, r, g, b, y, u, v, a = 0xff;
01053 if (c->srcFormat == PIX_FMT_PAL8) {
01054 p = ((const uint32_t *)(srcSlice[1]))[i];
01055 a = (p >> 24) & 0xFF;
01056 r = (p >> 16) & 0xFF;
01057 g = (p >> 8) & 0xFF;
01058 b = p & 0xFF;
01059 } else if (c->srcFormat == PIX_FMT_RGB8) {
01060 r = ( i >> 5 ) * 36;
01061 g = ((i >> 2) & 7) * 36;
01062 b = ( i & 3) * 85;
01063 } else if (c->srcFormat == PIX_FMT_BGR8) {
01064 b = ( i >> 6 ) * 85;
01065 g = ((i >> 3) & 7) * 36;
01066 r = ( i & 7) * 36;
01067 } else if (c->srcFormat == PIX_FMT_RGB4_BYTE) {
01068 r = ( i >> 3 ) * 255;
01069 g = ((i >> 1) & 3) * 85;
01070 b = ( i & 1) * 255;
01071 } else if (c->srcFormat == PIX_FMT_GRAY8 || c->srcFormat == PIX_FMT_GRAY8A) {
01072 r = g = b = i;
01073 } else {
01074 assert(c->srcFormat == PIX_FMT_BGR4_BYTE);
01075 b = ( i >> 3 ) * 255;
01076 g = ((i >> 1) & 3) * 85;
01077 r = ( i & 1) * 255;
01078 }
01079 y = av_clip_uint8((RY * r + GY * g + BY * b + ( 33 << (RGB2YUV_SHIFT - 1))) >> RGB2YUV_SHIFT);
01080 u = av_clip_uint8((RU * r + GU * g + BU * b + (257 << (RGB2YUV_SHIFT - 1))) >> RGB2YUV_SHIFT);
01081 v = av_clip_uint8((RV * r + GV * g + BV * b + (257 << (RGB2YUV_SHIFT - 1))) >> RGB2YUV_SHIFT);
01082 c->pal_yuv[i]= y + (u<<8) + (v<<16) + (a<<24);
01083
01084 switch (c->dstFormat) {
01085 case PIX_FMT_BGR32:
01086 #if !HAVE_BIGENDIAN
01087 case PIX_FMT_RGB24:
01088 #endif
01089 c->pal_rgb[i]= r + (g<<8) + (b<<16) + (a<<24);
01090 break;
01091 case PIX_FMT_BGR32_1:
01092 #if HAVE_BIGENDIAN
01093 case PIX_FMT_BGR24:
01094 #endif
01095 c->pal_rgb[i]= a + (r<<8) + (g<<16) + (b<<24);
01096 break;
01097 case PIX_FMT_RGB32_1:
01098 #if HAVE_BIGENDIAN
01099 case PIX_FMT_RGB24:
01100 #endif
01101 c->pal_rgb[i]= a + (b<<8) + (g<<16) + (r<<24);
01102 break;
01103 case PIX_FMT_RGB32:
01104 #if !HAVE_BIGENDIAN
01105 case PIX_FMT_BGR24:
01106 #endif
01107 default:
01108 c->pal_rgb[i]= b + (g<<8) + (r<<16) + (a<<24);
01109 }
01110 }
01111 }
01112
01113 if (c->src0Alpha && !c->dst0Alpha && isALPHA(c->dstFormat)) {
01114 uint8_t *base;
01115 int x,y;
01116 rgb0_tmp = av_malloc(FFABS(srcStride[0]) * srcSliceH + 32);
01117 base = srcStride[0] < 0 ? rgb0_tmp - srcStride[0] * (srcSliceH-1) : rgb0_tmp;
01118 for (y=0; y<srcSliceH; y++){
01119 memcpy(base + srcStride[0]*y, src2[0] + srcStride[0]*y, 4*c->srcW);
01120 for (x=c->src0Alpha-1; x<4*c->srcW; x+=4) {
01121 base[ srcStride[0]*y + x] = 0xFF;
01122 }
01123 }
01124 src2[0] = base;
01125 }
01126
01127
01128 if (c->sliceDir == 1) {
01129
01130 int srcStride2[4] = { srcStride[0], srcStride[1], srcStride[2],
01131 srcStride[3] };
01132 int dstStride2[4] = { dstStride[0], dstStride[1], dstStride[2],
01133 dstStride[3] };
01134
01135 reset_ptr(src2, c->srcFormat);
01136 reset_ptr((void*)dst2, c->dstFormat);
01137
01138
01139 if (srcSliceY + srcSliceH == c->srcH)
01140 c->sliceDir = 0;
01141
01142 ret = c->swScale(c, src2, srcStride2, srcSliceY, srcSliceH, dst2,
01143 dstStride2);
01144 } else {
01145
01146 int srcStride2[4] = { -srcStride[0], -srcStride[1], -srcStride[2],
01147 -srcStride[3] };
01148 int dstStride2[4] = { -dstStride[0], -dstStride[1], -dstStride[2],
01149 -dstStride[3] };
01150
01151 src2[0] += (srcSliceH - 1) * srcStride[0];
01152 if (!usePal(c->srcFormat))
01153 src2[1] += ((srcSliceH >> c->chrSrcVSubSample) - 1) * srcStride[1];
01154 src2[2] += ((srcSliceH >> c->chrSrcVSubSample) - 1) * srcStride[2];
01155 src2[3] += (srcSliceH - 1) * srcStride[3];
01156 dst2[0] += ( c->dstH - 1) * dstStride[0];
01157 dst2[1] += ((c->dstH >> c->chrDstVSubSample) - 1) * dstStride[1];
01158 dst2[2] += ((c->dstH >> c->chrDstVSubSample) - 1) * dstStride[2];
01159 dst2[3] += ( c->dstH - 1) * dstStride[3];
01160
01161 reset_ptr(src2, c->srcFormat);
01162 reset_ptr((void*)dst2, c->dstFormat);
01163
01164
01165 if (!srcSliceY)
01166 c->sliceDir = 0;
01167
01168 ret = c->swScale(c, src2, srcStride2, c->srcH-srcSliceY-srcSliceH,
01169 srcSliceH, dst2, dstStride2);
01170 }
01171
01172 av_free(rgb0_tmp);
01173 return ret;
01174 }
01175
01176
01177 void sws_convertPalette8ToPacked32(const uint8_t *src, uint8_t *dst,
01178 int num_pixels, const uint8_t *palette)
01179 {
01180 int i;
01181
01182 for (i = 0; i < num_pixels; i++)
01183 ((uint32_t *) dst)[i] = ((const uint32_t *) palette)[src[i]];
01184 }
01185
01186
01187 void sws_convertPalette8ToPacked24(const uint8_t *src, uint8_t *dst,
01188 int num_pixels, const uint8_t *palette)
01189 {
01190 int i;
01191
01192 for (i = 0; i < num_pixels; i++) {
01193
01194 dst[0] = palette[src[i] * 4 + 0];
01195 dst[1] = palette[src[i] * 4 + 1];
01196 dst[2] = palette[src[i] * 4 + 2];
01197 dst += 3;
01198 }
01199 }