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 static void fillPlane(uint8_t* plane, int stride, int width, int height, int y, uint8_t val)
00049 {
00050 int i;
00051 uint8_t *ptr = plane + stride*y;
00052 for (i=0; i<height; i++) {
00053 memset(ptr, val, width);
00054 ptr += stride;
00055 }
00056 }
00057
00058 static void copyPlane(const uint8_t *src, int srcStride,
00059 int srcSliceY, int srcSliceH, int width,
00060 uint8_t *dst, int dstStride)
00061 {
00062 dst += dstStride * srcSliceY;
00063 if (dstStride == srcStride && srcStride > 0) {
00064 memcpy(dst, src, srcSliceH * dstStride);
00065 } else {
00066 int i;
00067 for (i=0; i<srcSliceH; i++) {
00068 memcpy(dst, src, width);
00069 src += srcStride;
00070 dst += dstStride;
00071 }
00072 }
00073 }
00074
00075 static int planarToNv12Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
00076 int srcSliceH, uint8_t* dstParam[], int dstStride[])
00077 {
00078 uint8_t *dst = dstParam[1] + dstStride[1]*srcSliceY/2;
00079
00080 copyPlane(src[0], srcStride[0], srcSliceY, srcSliceH, c->srcW,
00081 dstParam[0], dstStride[0]);
00082
00083 if (c->dstFormat == PIX_FMT_NV12)
00084 interleaveBytes(src[1], src[2], dst, c->srcW/2, srcSliceH/2, srcStride[1], srcStride[2], dstStride[0]);
00085 else
00086 interleaveBytes(src[2], src[1], dst, c->srcW/2, srcSliceH/2, srcStride[2], srcStride[1], dstStride[0]);
00087
00088 return srcSliceH;
00089 }
00090
00091 static int planarToYuy2Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
00092 int srcSliceH, uint8_t* dstParam[], int dstStride[])
00093 {
00094 uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
00095
00096 yv12toyuy2(src[0], src[1], src[2], dst, c->srcW, srcSliceH, srcStride[0], srcStride[1], dstStride[0]);
00097
00098 return srcSliceH;
00099 }
00100
00101 static int planarToUyvyWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
00102 int srcSliceH, uint8_t* dstParam[], int dstStride[])
00103 {
00104 uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
00105
00106 yv12touyvy(src[0], src[1], src[2], dst, c->srcW, srcSliceH, srcStride[0], srcStride[1], dstStride[0]);
00107
00108 return srcSliceH;
00109 }
00110
00111 static int yuv422pToYuy2Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
00112 int srcSliceH, uint8_t* dstParam[], int dstStride[])
00113 {
00114 uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
00115
00116 yuv422ptoyuy2(src[0],src[1],src[2],dst,c->srcW,srcSliceH,srcStride[0],srcStride[1],dstStride[0]);
00117
00118 return srcSliceH;
00119 }
00120
00121 static int yuv422pToUyvyWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
00122 int srcSliceH, uint8_t* dstParam[], int dstStride[])
00123 {
00124 uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
00125
00126 yuv422ptouyvy(src[0],src[1],src[2],dst,c->srcW,srcSliceH,srcStride[0],srcStride[1],dstStride[0]);
00127
00128 return srcSliceH;
00129 }
00130
00131 static int yuyvToYuv420Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
00132 int srcSliceH, uint8_t* dstParam[], int dstStride[])
00133 {
00134 uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
00135 uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY/2;
00136 uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY/2;
00137
00138 yuyvtoyuv420(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
00139
00140 if (dstParam[3])
00141 fillPlane(dstParam[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
00142
00143 return srcSliceH;
00144 }
00145
00146 static int yuyvToYuv422Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
00147 int srcSliceH, uint8_t* dstParam[], int dstStride[])
00148 {
00149 uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
00150 uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY;
00151 uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY;
00152
00153 yuyvtoyuv422(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
00154
00155 return srcSliceH;
00156 }
00157
00158 static int uyvyToYuv420Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
00159 int srcSliceH, uint8_t* dstParam[], int dstStride[])
00160 {
00161 uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
00162 uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY/2;
00163 uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY/2;
00164
00165 uyvytoyuv420(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
00166
00167 if (dstParam[3])
00168 fillPlane(dstParam[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
00169
00170 return srcSliceH;
00171 }
00172
00173 static int uyvyToYuv422Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
00174 int srcSliceH, uint8_t* dstParam[], int dstStride[])
00175 {
00176 uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
00177 uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY;
00178 uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY;
00179
00180 uyvytoyuv422(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
00181
00182 return srcSliceH;
00183 }
00184
00185 static void gray8aToPacked32(const uint8_t *src, uint8_t *dst, int num_pixels, const uint8_t *palette)
00186 {
00187 int i;
00188 for (i=0; i<num_pixels; i++)
00189 ((uint32_t *) dst)[i] = ((const uint32_t *)palette)[src[i<<1]] | (src[(i<<1)+1] << 24);
00190 }
00191
00192 static void gray8aToPacked32_1(const uint8_t *src, uint8_t *dst, int num_pixels, const uint8_t *palette)
00193 {
00194 int i;
00195
00196 for (i=0; i<num_pixels; i++)
00197 ((uint32_t *) dst)[i] = ((const uint32_t *)palette)[src[i<<1]] | src[(i<<1)+1];
00198 }
00199
00200 static void gray8aToPacked24(const uint8_t *src, uint8_t *dst, int num_pixels, const uint8_t *palette)
00201 {
00202 int i;
00203
00204 for (i=0; i<num_pixels; i++) {
00205
00206 dst[0]= palette[src[i<<1]*4+0];
00207 dst[1]= palette[src[i<<1]*4+1];
00208 dst[2]= palette[src[i<<1]*4+2];
00209 dst+= 3;
00210 }
00211 }
00212
00213 static int palToRgbWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
00214 int srcSliceH, uint8_t* dst[], int dstStride[])
00215 {
00216 const enum PixelFormat srcFormat= c->srcFormat;
00217 const enum PixelFormat dstFormat= c->dstFormat;
00218 void (*conv)(const uint8_t *src, uint8_t *dst, int num_pixels,
00219 const uint8_t *palette)=NULL;
00220 int i;
00221 uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
00222 const uint8_t *srcPtr= src[0];
00223
00224 if (srcFormat == PIX_FMT_GRAY8A) {
00225 switch (dstFormat) {
00226 case PIX_FMT_RGB32 : conv = gray8aToPacked32; break;
00227 case PIX_FMT_BGR32 : conv = gray8aToPacked32; break;
00228 case PIX_FMT_BGR32_1: conv = gray8aToPacked32_1; break;
00229 case PIX_FMT_RGB32_1: conv = gray8aToPacked32_1; break;
00230 case PIX_FMT_RGB24 : conv = gray8aToPacked24; break;
00231 case PIX_FMT_BGR24 : conv = gray8aToPacked24; break;
00232 }
00233 } else if (usePal(srcFormat)) {
00234 switch (dstFormat) {
00235 case PIX_FMT_RGB32 : conv = sws_convertPalette8ToPacked32; break;
00236 case PIX_FMT_BGR32 : conv = sws_convertPalette8ToPacked32; break;
00237 case PIX_FMT_BGR32_1: conv = sws_convertPalette8ToPacked32; break;
00238 case PIX_FMT_RGB32_1: conv = sws_convertPalette8ToPacked32; break;
00239 case PIX_FMT_RGB24 : conv = sws_convertPalette8ToPacked24; break;
00240 case PIX_FMT_BGR24 : conv = sws_convertPalette8ToPacked24; break;
00241 }
00242 }
00243
00244 if (!conv)
00245 av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
00246 av_get_pix_fmt_name(srcFormat), av_get_pix_fmt_name(dstFormat));
00247 else {
00248 for (i=0; i<srcSliceH; i++) {
00249 conv(srcPtr, dstPtr, c->srcW, (uint8_t *) c->pal_rgb);
00250 srcPtr+= srcStride[0];
00251 dstPtr+= dstStride[0];
00252 }
00253 }
00254
00255 return srcSliceH;
00256 }
00257
00258 #define isRGBA32(x) ( \
00259 (x) == PIX_FMT_ARGB \
00260 || (x) == PIX_FMT_RGBA \
00261 || (x) == PIX_FMT_BGRA \
00262 || (x) == PIX_FMT_ABGR \
00263 )
00264
00265
00266 static int rgbToRgbWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
00267 int srcSliceH, uint8_t* dst[], int dstStride[])
00268 {
00269 const enum PixelFormat srcFormat= c->srcFormat;
00270 const enum PixelFormat dstFormat= c->dstFormat;
00271 const int srcBpp= (c->srcFormatBpp + 7) >> 3;
00272 const int dstBpp= (c->dstFormatBpp + 7) >> 3;
00273 const int srcId= c->srcFormatBpp >> 2;
00274 const int dstId= c->dstFormatBpp >> 2;
00275 void (*conv)(const uint8_t *src, uint8_t *dst, int src_size)=NULL;
00276
00277 #define CONV_IS(src, dst) (srcFormat == PIX_FMT_##src && dstFormat == PIX_FMT_##dst)
00278
00279 if (isRGBA32(srcFormat) && isRGBA32(dstFormat)) {
00280 if ( CONV_IS(ABGR, RGBA)
00281 || CONV_IS(ARGB, BGRA)
00282 || CONV_IS(BGRA, ARGB)
00283 || CONV_IS(RGBA, ABGR)) conv = shuffle_bytes_3210;
00284 else if (CONV_IS(ABGR, ARGB)
00285 || CONV_IS(ARGB, ABGR)) conv = shuffle_bytes_0321;
00286 else if (CONV_IS(ABGR, BGRA)
00287 || CONV_IS(ARGB, RGBA)) conv = shuffle_bytes_1230;
00288 else if (CONV_IS(BGRA, RGBA)
00289 || CONV_IS(RGBA, BGRA)) conv = shuffle_bytes_2103;
00290 else if (CONV_IS(BGRA, ABGR)
00291 || CONV_IS(RGBA, ARGB)) conv = shuffle_bytes_3012;
00292 } else
00293
00294 if ( (isBGRinInt(srcFormat) && isBGRinInt(dstFormat))
00295 || (isRGBinInt(srcFormat) && isRGBinInt(dstFormat))) {
00296 switch(srcId | (dstId<<4)) {
00297 case 0x34: conv= rgb16to15; break;
00298 case 0x36: conv= rgb24to15; break;
00299 case 0x38: conv= rgb32to15; break;
00300 case 0x43: conv= rgb15to16; break;
00301 case 0x46: conv= rgb24to16; break;
00302 case 0x48: conv= rgb32to16; break;
00303 case 0x63: conv= rgb15to24; break;
00304 case 0x64: conv= rgb16to24; break;
00305 case 0x68: conv= rgb32to24; break;
00306 case 0x83: conv= rgb15to32; break;
00307 case 0x84: conv= rgb16to32; break;
00308 case 0x86: conv= rgb24to32; break;
00309 }
00310 } else if ( (isBGRinInt(srcFormat) && isRGBinInt(dstFormat))
00311 || (isRGBinInt(srcFormat) && isBGRinInt(dstFormat))) {
00312 switch(srcId | (dstId<<4)) {
00313 case 0x33: conv= rgb15tobgr15; break;
00314 case 0x34: conv= rgb16tobgr15; break;
00315 case 0x36: conv= rgb24tobgr15; break;
00316 case 0x38: conv= rgb32tobgr15; break;
00317 case 0x43: conv= rgb15tobgr16; break;
00318 case 0x44: conv= rgb16tobgr16; break;
00319 case 0x46: conv= rgb24tobgr16; break;
00320 case 0x48: conv= rgb32tobgr16; break;
00321 case 0x63: conv= rgb15tobgr24; break;
00322 case 0x64: conv= rgb16tobgr24; break;
00323 case 0x66: conv= rgb24tobgr24; break;
00324 case 0x68: conv= rgb32tobgr24; break;
00325 case 0x83: conv= rgb15tobgr32; break;
00326 case 0x84: conv= rgb16tobgr32; break;
00327 case 0x86: conv= rgb24tobgr32; break;
00328 }
00329 }
00330
00331 if (!conv) {
00332 av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
00333 av_get_pix_fmt_name(srcFormat), av_get_pix_fmt_name(dstFormat));
00334 } else {
00335 const uint8_t *srcPtr= src[0];
00336 uint8_t *dstPtr= dst[0];
00337 if ((srcFormat == PIX_FMT_RGB32_1 || srcFormat == PIX_FMT_BGR32_1) && !isRGBA32(dstFormat))
00338 srcPtr += ALT32_CORR;
00339
00340 if ((dstFormat == PIX_FMT_RGB32_1 || dstFormat == PIX_FMT_BGR32_1) && !isRGBA32(srcFormat))
00341 dstPtr += ALT32_CORR;
00342
00343 if (dstStride[0]*srcBpp == srcStride[0]*dstBpp && srcStride[0] > 0 && !(srcStride[0]%srcBpp))
00344 conv(srcPtr, dstPtr + dstStride[0]*srcSliceY, srcSliceH*srcStride[0]);
00345 else {
00346 int i;
00347 dstPtr += dstStride[0]*srcSliceY;
00348
00349 for (i=0; i<srcSliceH; i++) {
00350 conv(srcPtr, dstPtr, c->srcW*srcBpp);
00351 srcPtr+= srcStride[0];
00352 dstPtr+= dstStride[0];
00353 }
00354 }
00355 }
00356 return srcSliceH;
00357 }
00358
00359 static int bgr24ToYv12Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
00360 int srcSliceH, uint8_t* dst[], int dstStride[])
00361 {
00362 rgb24toyv12(
00363 src[0],
00364 dst[0]+ srcSliceY *dstStride[0],
00365 dst[1]+(srcSliceY>>1)*dstStride[1],
00366 dst[2]+(srcSliceY>>1)*dstStride[2],
00367 c->srcW, srcSliceH,
00368 dstStride[0], dstStride[1], srcStride[0]);
00369 if (dst[3])
00370 fillPlane(dst[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
00371 return srcSliceH;
00372 }
00373
00374 static int yvu9ToYv12Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
00375 int srcSliceH, uint8_t* dst[], int dstStride[])
00376 {
00377 copyPlane(src[0], srcStride[0], srcSliceY, srcSliceH, c->srcW,
00378 dst[0], dstStride[0]);
00379
00380 planar2x(src[1], dst[1] + dstStride[1]*(srcSliceY >> 1), c->chrSrcW,
00381 srcSliceH >> 2, srcStride[1], dstStride[1]);
00382 planar2x(src[2], dst[2] + dstStride[2]*(srcSliceY >> 1), c->chrSrcW,
00383 srcSliceH >> 2, srcStride[2], dstStride[2]);
00384 if (dst[3])
00385 fillPlane(dst[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
00386 return srcSliceH;
00387 }
00388
00389
00390 static int packedCopyWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
00391 int srcSliceH, uint8_t* dst[], int dstStride[])
00392 {
00393 if (dstStride[0]==srcStride[0] && srcStride[0] > 0)
00394 memcpy(dst[0] + dstStride[0]*srcSliceY, src[0], srcSliceH*dstStride[0]);
00395 else {
00396 int i;
00397 const uint8_t *srcPtr= src[0];
00398 uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
00399 int length=0;
00400
00401
00402 while(length+c->srcW <= FFABS(dstStride[0])
00403 && length+c->srcW <= FFABS(srcStride[0])) length+= c->srcW;
00404 assert(length!=0);
00405
00406 for (i=0; i<srcSliceH; i++) {
00407 memcpy(dstPtr, srcPtr, length);
00408 srcPtr+= srcStride[0];
00409 dstPtr+= dstStride[0];
00410 }
00411 }
00412 return srcSliceH;
00413 }
00414
00415 #define DITHER_COPY(dst, dstStride, src, srcStride, bswap, dbswap)\
00416 uint16_t scale= dither_scale[dst_depth-1][src_depth-1];\
00417 int shift= src_depth-dst_depth + dither_scale[src_depth-2][dst_depth-1];\
00418 for (i = 0; i < height; i++) {\
00419 const uint8_t *dither= dithers[src_depth-9][i&7];\
00420 for (j = 0; j < length-7; j+=8){\
00421 dst[j+0] = dbswap((bswap(src[j+0]) + dither[0])*scale>>shift);\
00422 dst[j+1] = dbswap((bswap(src[j+1]) + dither[1])*scale>>shift);\
00423 dst[j+2] = dbswap((bswap(src[j+2]) + dither[2])*scale>>shift);\
00424 dst[j+3] = dbswap((bswap(src[j+3]) + dither[3])*scale>>shift);\
00425 dst[j+4] = dbswap((bswap(src[j+4]) + dither[4])*scale>>shift);\
00426 dst[j+5] = dbswap((bswap(src[j+5]) + dither[5])*scale>>shift);\
00427 dst[j+6] = dbswap((bswap(src[j+6]) + dither[6])*scale>>shift);\
00428 dst[j+7] = dbswap((bswap(src[j+7]) + dither[7])*scale>>shift);\
00429 }\
00430 for (; j < length; j++)\
00431 dst[j] = dbswap((bswap(src[j]) + dither[j&7])*scale>>shift);\
00432 dst += dstStride;\
00433 src += srcStride;\
00434 }
00435
00436
00437 static int planarCopyWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
00438 int srcSliceH, uint8_t* dst[], int dstStride[])
00439 {
00440 int plane, i, j;
00441 for (plane=0; plane<4; plane++) {
00442 int length= (plane==0 || plane==3) ? c->srcW : -((-c->srcW )>>c->chrDstHSubSample);
00443 int y= (plane==0 || plane==3) ? srcSliceY: -((-srcSliceY)>>c->chrDstVSubSample);
00444 int height= (plane==0 || plane==3) ? srcSliceH: -((-srcSliceH)>>c->chrDstVSubSample);
00445 const uint8_t *srcPtr= src[plane];
00446 uint8_t *dstPtr= dst[plane] + dstStride[plane]*y;
00447
00448 if (!dst[plane]) continue;
00449
00450 if (plane == 1 && !dst[2]) continue;
00451 if (!src[plane] || (plane == 1 && !src[2])) {
00452 if(is16BPS(c->dstFormat))
00453 length*=2;
00454 fillPlane(dst[plane], dstStride[plane], length, height, y, (plane==3) ? 255 : 128);
00455 } else {
00456 if(isNBPS(c->srcFormat) || isNBPS(c->dstFormat)
00457 || (is16BPS(c->srcFormat) != is16BPS(c->dstFormat))
00458 ) {
00459 const int src_depth = av_pix_fmt_descriptors[c->srcFormat].comp[plane].depth_minus1+1;
00460 const int dst_depth = av_pix_fmt_descriptors[c->dstFormat].comp[plane].depth_minus1+1;
00461 const uint16_t *srcPtr2 = (const uint16_t*)srcPtr;
00462 uint16_t *dstPtr2 = (uint16_t*)dstPtr;
00463
00464 if (dst_depth == 8) {
00465 if(isBE(c->srcFormat) == HAVE_BIGENDIAN){
00466 DITHER_COPY(dstPtr, dstStride[plane], srcPtr2, srcStride[plane]/2, , )
00467 } else {
00468 DITHER_COPY(dstPtr, dstStride[plane], srcPtr2, srcStride[plane]/2, av_bswap16, )
00469 }
00470 } else if (src_depth == 8) {
00471 for (i = 0; i < height; i++) {
00472 if(isBE(c->dstFormat)){
00473 for (j = 0; j < length; j++)
00474 AV_WB16(&dstPtr2[j], (srcPtr[j]<<(dst_depth-8)) |
00475 (srcPtr[j]>>(2*8-dst_depth)));
00476 } else {
00477 for (j = 0; j < length; j++)
00478 AV_WL16(&dstPtr2[j], (srcPtr[j]<<(dst_depth-8)) |
00479 (srcPtr[j]>>(2*8-dst_depth)));
00480 }
00481 dstPtr2 += dstStride[plane]/2;
00482 srcPtr += srcStride[plane];
00483 }
00484 } else if (src_depth <= dst_depth) {
00485 for (i = 0; i < height; i++) {
00486 #define COPY_UP(r,w) \
00487 for (j = 0; j < length; j++){ \
00488 unsigned int v= r(&srcPtr2[j]);\
00489 w(&dstPtr2[j], (v<<(dst_depth-src_depth)) | \
00490 (v>>(2*src_depth-dst_depth)));\
00491 }
00492 if(isBE(c->srcFormat)){
00493 if(isBE(c->dstFormat)){
00494 COPY_UP(AV_RB16, AV_WB16)
00495 } else {
00496 COPY_UP(AV_RB16, AV_WL16)
00497 }
00498 } else {
00499 if(isBE(c->dstFormat)){
00500 COPY_UP(AV_RL16, AV_WB16)
00501 } else {
00502 COPY_UP(AV_RL16, AV_WL16)
00503 }
00504 }
00505 dstPtr2 += dstStride[plane]/2;
00506 srcPtr2 += srcStride[plane]/2;
00507 }
00508 } else {
00509 if(isBE(c->srcFormat) == HAVE_BIGENDIAN){
00510 if(isBE(c->dstFormat) == HAVE_BIGENDIAN){
00511 DITHER_COPY(dstPtr2, dstStride[plane]/2, srcPtr2, srcStride[plane]/2, , )
00512 } else {
00513 DITHER_COPY(dstPtr2, dstStride[plane]/2, srcPtr2, srcStride[plane]/2, , av_bswap16)
00514 }
00515 }else{
00516 if(isBE(c->dstFormat) == HAVE_BIGENDIAN){
00517 DITHER_COPY(dstPtr2, dstStride[plane]/2, srcPtr2, srcStride[plane]/2, av_bswap16, )
00518 } else {
00519 DITHER_COPY(dstPtr2, dstStride[plane]/2, srcPtr2, srcStride[plane]/2, av_bswap16, av_bswap16)
00520 }
00521 }
00522 }
00523 } else if(is16BPS(c->srcFormat) && is16BPS(c->dstFormat)
00524 && isBE(c->srcFormat) != isBE(c->dstFormat)) {
00525
00526 for (i=0; i<height; i++) {
00527 for (j=0; j<length; j++)
00528 ((uint16_t*)dstPtr)[j] = av_bswap16(((const uint16_t*)srcPtr)[j]);
00529 srcPtr+= srcStride[plane];
00530 dstPtr+= dstStride[plane];
00531 }
00532 } else if (dstStride[plane] == srcStride[plane] &&
00533 srcStride[plane] > 0 && srcStride[plane] == length) {
00534 memcpy(dst[plane] + dstStride[plane]*y, src[plane],
00535 height*dstStride[plane]);
00536 } else {
00537 if(is16BPS(c->srcFormat) && is16BPS(c->dstFormat))
00538 length*=2;
00539 for (i=0; i<height; i++) {
00540 memcpy(dstPtr, srcPtr, length);
00541 srcPtr+= srcStride[plane];
00542 dstPtr+= dstStride[plane];
00543 }
00544 }
00545 }
00546 }
00547 return srcSliceH;
00548 }
00549
00550 void ff_get_unscaled_swscale(SwsContext *c)
00551 {
00552 const enum PixelFormat srcFormat = c->srcFormat;
00553 const enum PixelFormat dstFormat = c->dstFormat;
00554 const int flags = c->flags;
00555 const int dstH = c->dstH;
00556 int needsDither;
00557
00558 needsDither= isAnyRGB(dstFormat)
00559 && c->dstFormatBpp < 24
00560 && (c->dstFormatBpp < c->srcFormatBpp || (!isAnyRGB(srcFormat)));
00561
00562
00563 if ((srcFormat == PIX_FMT_YUV420P || srcFormat == PIX_FMT_YUVA420P) && (dstFormat == PIX_FMT_NV12 || dstFormat == PIX_FMT_NV21)) {
00564 c->swScale= planarToNv12Wrapper;
00565 }
00566
00567 if ((srcFormat==PIX_FMT_YUV420P || srcFormat==PIX_FMT_YUV422P || srcFormat==PIX_FMT_YUVA420P) && isAnyRGB(dstFormat)
00568 && !(flags & SWS_ACCURATE_RND) && !(dstH&1)) {
00569 c->swScale= ff_yuv2rgb_get_func_ptr(c);
00570 }
00571
00572 if (srcFormat==PIX_FMT_YUV410P && (dstFormat==PIX_FMT_YUV420P || dstFormat==PIX_FMT_YUVA420P) && !(flags & SWS_BITEXACT)) {
00573 c->swScale= yvu9ToYv12Wrapper;
00574 }
00575
00576
00577 if (srcFormat==PIX_FMT_BGR24 && (dstFormat==PIX_FMT_YUV420P || dstFormat==PIX_FMT_YUVA420P) && !(flags & SWS_ACCURATE_RND))
00578 c->swScale= bgr24ToYv12Wrapper;
00579
00580
00581 if ( isAnyRGB(srcFormat)
00582 && isAnyRGB(dstFormat)
00583 && srcFormat != PIX_FMT_BGR8 && dstFormat != PIX_FMT_BGR8
00584 && srcFormat != PIX_FMT_RGB8 && dstFormat != PIX_FMT_RGB8
00585 && srcFormat != PIX_FMT_BGR4 && dstFormat != PIX_FMT_BGR4
00586 && srcFormat != PIX_FMT_RGB4 && dstFormat != PIX_FMT_RGB4
00587 && srcFormat != PIX_FMT_BGR4_BYTE && dstFormat != PIX_FMT_BGR4_BYTE
00588 && srcFormat != PIX_FMT_RGB4_BYTE && dstFormat != PIX_FMT_RGB4_BYTE
00589 && srcFormat != PIX_FMT_MONOBLACK && dstFormat != PIX_FMT_MONOBLACK
00590 && srcFormat != PIX_FMT_MONOWHITE && dstFormat != PIX_FMT_MONOWHITE
00591 && srcFormat != PIX_FMT_RGB48LE && dstFormat != PIX_FMT_RGB48LE
00592 && srcFormat != PIX_FMT_RGB48BE && dstFormat != PIX_FMT_RGB48BE
00593 && srcFormat != PIX_FMT_BGR48LE && dstFormat != PIX_FMT_BGR48LE
00594 && srcFormat != PIX_FMT_BGR48BE && dstFormat != PIX_FMT_BGR48BE
00595 && (!needsDither || (c->flags&(SWS_FAST_BILINEAR|SWS_POINT))))
00596 c->swScale= rgbToRgbWrapper;
00597
00598 if ((usePal(srcFormat) && (
00599 dstFormat == PIX_FMT_RGB32 ||
00600 dstFormat == PIX_FMT_RGB32_1 ||
00601 dstFormat == PIX_FMT_RGB24 ||
00602 dstFormat == PIX_FMT_BGR32 ||
00603 dstFormat == PIX_FMT_BGR32_1 ||
00604 dstFormat == PIX_FMT_BGR24)))
00605 c->swScale= palToRgbWrapper;
00606
00607 if (srcFormat == PIX_FMT_YUV422P) {
00608 if (dstFormat == PIX_FMT_YUYV422)
00609 c->swScale= yuv422pToYuy2Wrapper;
00610 else if (dstFormat == PIX_FMT_UYVY422)
00611 c->swScale= yuv422pToUyvyWrapper;
00612 }
00613
00614
00615 if (c->flags&(SWS_FAST_BILINEAR|SWS_POINT)) {
00616
00617 if (srcFormat == PIX_FMT_YUV420P || srcFormat == PIX_FMT_YUVA420P) {
00618 if (dstFormat == PIX_FMT_YUYV422)
00619 c->swScale= planarToYuy2Wrapper;
00620 else if (dstFormat == PIX_FMT_UYVY422)
00621 c->swScale= planarToUyvyWrapper;
00622 }
00623 }
00624 if(srcFormat == PIX_FMT_YUYV422 && (dstFormat == PIX_FMT_YUV420P || dstFormat == PIX_FMT_YUVA420P))
00625 c->swScale= yuyvToYuv420Wrapper;
00626 if(srcFormat == PIX_FMT_UYVY422 && (dstFormat == PIX_FMT_YUV420P || dstFormat == PIX_FMT_YUVA420P))
00627 c->swScale= uyvyToYuv420Wrapper;
00628 if(srcFormat == PIX_FMT_YUYV422 && dstFormat == PIX_FMT_YUV422P)
00629 c->swScale= yuyvToYuv422Wrapper;
00630 if(srcFormat == PIX_FMT_UYVY422 && dstFormat == PIX_FMT_YUV422P)
00631 c->swScale= uyvyToYuv422Wrapper;
00632
00633
00634 if ( srcFormat == dstFormat
00635 || (srcFormat == PIX_FMT_YUVA420P && dstFormat == PIX_FMT_YUV420P)
00636 || (srcFormat == PIX_FMT_YUV420P && dstFormat == PIX_FMT_YUVA420P)
00637 || (isPlanarYUV(srcFormat) && isGray(dstFormat))
00638 || (isPlanarYUV(dstFormat) && isGray(srcFormat))
00639 || (isGray(dstFormat) && isGray(srcFormat))
00640 || (isPlanarYUV(srcFormat) && isPlanarYUV(dstFormat)
00641 && c->chrDstHSubSample == c->chrSrcHSubSample
00642 && c->chrDstVSubSample == c->chrSrcVSubSample
00643 && dstFormat != PIX_FMT_NV12 && dstFormat != PIX_FMT_NV21
00644 && srcFormat != PIX_FMT_NV12 && srcFormat != PIX_FMT_NV21))
00645 {
00646 if (isPacked(c->srcFormat))
00647 c->swScale= packedCopyWrapper;
00648 else
00649 c->swScale= planarCopyWrapper;
00650 }
00651
00652 if (ARCH_BFIN)
00653 ff_bfin_get_unscaled_swscale(c);
00654 if (HAVE_ALTIVEC)
00655 ff_swscale_get_unscaled_altivec(c);
00656 }
00657
00658 static void reset_ptr(const uint8_t* src[], int format)
00659 {
00660 if(!isALPHA(format))
00661 src[3]=NULL;
00662 if(!isPlanarYUV(format)) {
00663 src[3]=src[2]=NULL;
00664
00665 if (!usePal(format))
00666 src[1]= NULL;
00667 }
00668 }
00669
00670 static int check_image_pointers(uint8_t *data[4], enum PixelFormat pix_fmt,
00671 const int linesizes[4])
00672 {
00673 const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
00674 int i;
00675
00676 for (i = 0; i < 4; i++) {
00677 int plane = desc->comp[i].plane;
00678 if (!data[plane] || !linesizes[plane])
00679 return 0;
00680 }
00681
00682 return 1;
00683 }
00684
00689 int sws_scale(SwsContext *c, const uint8_t* const src[], const int srcStride[], int srcSliceY,
00690 int srcSliceH, uint8_t* const dst[], const int dstStride[])
00691 {
00692 int i;
00693 const uint8_t* src2[4]= {src[0], src[1], src[2], src[3]};
00694 uint8_t* dst2[4]= {dst[0], dst[1], dst[2], dst[3]};
00695
00696
00697 if (srcSliceH == 0)
00698 return 0;
00699
00700 if (!check_image_pointers(src, c->srcFormat, srcStride)) {
00701 av_log(c, AV_LOG_ERROR, "bad src image pointers\n");
00702 return 0;
00703 }
00704 if (!check_image_pointers(dst, c->dstFormat, dstStride)) {
00705 av_log(c, AV_LOG_ERROR, "bad dst image pointers\n");
00706 return 0;
00707 }
00708
00709 if (c->sliceDir == 0 && srcSliceY != 0 && srcSliceY + srcSliceH != c->srcH) {
00710 av_log(c, AV_LOG_ERROR, "Slices start in the middle!\n");
00711 return 0;
00712 }
00713 if (c->sliceDir == 0) {
00714 if (srcSliceY == 0) c->sliceDir = 1; else c->sliceDir = -1;
00715 }
00716
00717 if (usePal(c->srcFormat)) {
00718 for (i=0; i<256; i++) {
00719 int p, r, g, b, y, u, v, a = 0xff;
00720 if(c->srcFormat == PIX_FMT_PAL8) {
00721 p=((const uint32_t*)(src[1]))[i];
00722 a= (p>>24)&0xFF;
00723 r= (p>>16)&0xFF;
00724 g= (p>> 8)&0xFF;
00725 b= p &0xFF;
00726 } else if(c->srcFormat == PIX_FMT_RGB8) {
00727 r= (i>>5 )*36;
00728 g= ((i>>2)&7)*36;
00729 b= (i&3 )*85;
00730 } else if(c->srcFormat == PIX_FMT_BGR8) {
00731 b= (i>>6 )*85;
00732 g= ((i>>3)&7)*36;
00733 r= (i&7 )*36;
00734 } else if(c->srcFormat == PIX_FMT_RGB4_BYTE) {
00735 r= (i>>3 )*255;
00736 g= ((i>>1)&3)*85;
00737 b= (i&1 )*255;
00738 } else if(c->srcFormat == PIX_FMT_GRAY8 || c->srcFormat == PIX_FMT_GRAY8A) {
00739 r = g = b = i;
00740 } else {
00741 assert(c->srcFormat == PIX_FMT_BGR4_BYTE);
00742 b= (i>>3 )*255;
00743 g= ((i>>1)&3)*85;
00744 r= (i&1 )*255;
00745 }
00746 y= av_clip_uint8((RY*r + GY*g + BY*b + ( 33<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
00747 u= av_clip_uint8((RU*r + GU*g + BU*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
00748 v= av_clip_uint8((RV*r + GV*g + BV*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
00749 c->pal_yuv[i]= y + (u<<8) + (v<<16) + (a<<24);
00750
00751 switch(c->dstFormat) {
00752 case PIX_FMT_BGR32:
00753 #if !HAVE_BIGENDIAN
00754 case PIX_FMT_RGB24:
00755 #endif
00756 c->pal_rgb[i]= r + (g<<8) + (b<<16) + (a<<24);
00757 break;
00758 case PIX_FMT_BGR32_1:
00759 #if HAVE_BIGENDIAN
00760 case PIX_FMT_BGR24:
00761 #endif
00762 c->pal_rgb[i]= a + (r<<8) + (g<<16) + (b<<24);
00763 break;
00764 case PIX_FMT_RGB32_1:
00765 #if HAVE_BIGENDIAN
00766 case PIX_FMT_RGB24:
00767 #endif
00768 c->pal_rgb[i]= a + (b<<8) + (g<<16) + (r<<24);
00769 break;
00770 case PIX_FMT_RGB32:
00771 #if !HAVE_BIGENDIAN
00772 case PIX_FMT_BGR24:
00773 #endif
00774 default:
00775 c->pal_rgb[i]= b + (g<<8) + (r<<16) + (a<<24);
00776 }
00777 }
00778 }
00779
00780
00781 if (c->sliceDir == 1) {
00782
00783 int srcStride2[4]= {srcStride[0], srcStride[1], srcStride[2], srcStride[3]};
00784 int dstStride2[4]= {dstStride[0], dstStride[1], dstStride[2], dstStride[3]};
00785
00786 reset_ptr(src2, c->srcFormat);
00787 reset_ptr((const uint8_t**)dst2, c->dstFormat);
00788
00789
00790 if (srcSliceY + srcSliceH == c->srcH)
00791 c->sliceDir = 0;
00792
00793 return c->swScale(c, src2, srcStride2, srcSliceY, srcSliceH, dst2, dstStride2);
00794 } else {
00795
00796 int srcStride2[4]= {-srcStride[0], -srcStride[1], -srcStride[2], -srcStride[3]};
00797 int dstStride2[4]= {-dstStride[0], -dstStride[1], -dstStride[2], -dstStride[3]};
00798
00799 src2[0] += (srcSliceH-1)*srcStride[0];
00800 if (!usePal(c->srcFormat))
00801 src2[1] += ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[1];
00802 src2[2] += ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[2];
00803 src2[3] += (srcSliceH-1)*srcStride[3];
00804 dst2[0] += ( c->dstH -1)*dstStride[0];
00805 dst2[1] += ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[1];
00806 dst2[2] += ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[2];
00807 dst2[3] += ( c->dstH -1)*dstStride[3];
00808
00809 reset_ptr(src2, c->srcFormat);
00810 reset_ptr((const uint8_t**)dst2, c->dstFormat);
00811
00812
00813 if (!srcSliceY)
00814 c->sliceDir = 0;
00815
00816 return c->swScale(c, src2, srcStride2, c->srcH-srcSliceY-srcSliceH, srcSliceH, dst2, dstStride2);
00817 }
00818 }
00819
00820 #if LIBSWSCALE_VERSION_MAJOR < 1
00821 int sws_scale_ordered(SwsContext *c, const uint8_t* const src[], int srcStride[], int srcSliceY,
00822 int srcSliceH, uint8_t* dst[], int dstStride[])
00823 {
00824 return sws_scale(c, src, srcStride, srcSliceY, srcSliceH, dst, dstStride);
00825 }
00826 #endif
00827
00828
00829 void sws_convertPalette8ToPacked32(const uint8_t *src, uint8_t *dst, int num_pixels, const uint8_t *palette)
00830 {
00831 int i;
00832
00833 for (i=0; i<num_pixels; i++)
00834 ((uint32_t *) dst)[i] = ((const uint32_t *) palette)[src[i]];
00835 }
00836
00837
00838 void sws_convertPalette8ToPacked24(const uint8_t *src, uint8_t *dst, int num_pixels, const uint8_t *palette)
00839 {
00840 int i;
00841
00842 for (i=0; i<num_pixels; i++) {
00843
00844 dst[0]= palette[src[i]*4+0];
00845 dst[1]= palette[src[i]*4+1];
00846 dst[2]= palette[src[i]*4+2];
00847 dst+= 3;
00848 }
00849 }