diff options
Diffstat (limited to 'ffmpeg/libswscale/utils.c')
| -rw-r--r-- | ffmpeg/libswscale/utils.c | 541 |
1 files changed, 376 insertions, 165 deletions
diff --git a/ffmpeg/libswscale/utils.c b/ffmpeg/libswscale/utils.c index 932cf94..b8b95a9 100644 --- a/ffmpeg/libswscale/utils.c +++ b/ffmpeg/libswscale/utils.c @@ -46,12 +46,15 @@ #include "libavutil/mathematics.h" #include "libavutil/opt.h" #include "libavutil/pixdesc.h" +#include "libavutil/ppc/cpu.h" #include "libavutil/x86/asm.h" #include "libavutil/x86/cpu.h" #include "rgb2rgb.h" #include "swscale.h" #include "swscale_internal.h" +static void handle_formats(SwsContext *c); + unsigned swscale_version(void) { av_assert0(LIBSWSCALE_VERSION_MICRO >= 100); @@ -72,7 +75,9 @@ const char *swscale_license(void) #define RET 0xC3 // near return opcode for x86 typedef struct FormatEntry { - int is_supported_in, is_supported_out; + uint8_t is_supported_in :1; + uint8_t is_supported_out :1; + uint8_t is_supported_endianness :1; } FormatEntry; static const FormatEntry format_entries[AV_PIX_FMT_NB] = { @@ -89,6 +94,7 @@ static const FormatEntry format_entries[AV_PIX_FMT_NB] = { [AV_PIX_FMT_MONOBLACK] = { 1, 1 }, [AV_PIX_FMT_PAL8] = { 1, 0 }, [AV_PIX_FMT_YUVJ420P] = { 1, 1 }, + [AV_PIX_FMT_YUVJ411P] = { 1, 1 }, [AV_PIX_FMT_YUVJ422P] = { 1, 1 }, [AV_PIX_FMT_YUVJ444P] = { 1, 1 }, [AV_PIX_FMT_UYVY422] = { 1, 1 }, @@ -136,8 +142,8 @@ static const FormatEntry format_entries[AV_PIX_FMT_NB] = { [AV_PIX_FMT_YUVA444P16LE]= { 1, 1 }, [AV_PIX_FMT_RGB48BE] = { 1, 1 }, [AV_PIX_FMT_RGB48LE] = { 1, 1 }, - [AV_PIX_FMT_RGBA64BE] = { 1, 0 }, - [AV_PIX_FMT_RGBA64LE] = { 1, 0 }, + [AV_PIX_FMT_RGBA64BE] = { 1, 1 }, + [AV_PIX_FMT_RGBA64LE] = { 1, 1 }, [AV_PIX_FMT_RGB565BE] = { 1, 1 }, [AV_PIX_FMT_RGB565LE] = { 1, 1 }, [AV_PIX_FMT_RGB555BE] = { 1, 1 }, @@ -196,6 +202,11 @@ static const FormatEntry format_entries[AV_PIX_FMT_NB] = { [AV_PIX_FMT_GBRP14BE] = { 1, 1 }, [AV_PIX_FMT_GBRP16LE] = { 1, 0 }, [AV_PIX_FMT_GBRP16BE] = { 1, 0 }, + [AV_PIX_FMT_XYZ12BE] = { 1, 1, 1 }, + [AV_PIX_FMT_XYZ12LE] = { 1, 1, 1 }, + [AV_PIX_FMT_GBRAP] = { 1, 1 }, + [AV_PIX_FMT_GBRAP16LE] = { 1, 0 }, + [AV_PIX_FMT_GBRAP16BE] = { 1, 0 }, }; int sws_isSupportedInput(enum AVPixelFormat pix_fmt) @@ -210,7 +221,11 @@ int sws_isSupportedOutput(enum AVPixelFormat pix_fmt) format_entries[pix_fmt].is_supported_out : 0; } -extern const int32_t ff_yuv2rgb_coeffs[8][4]; +int sws_isSupportedEndiannessConversion(enum AVPixelFormat pix_fmt) +{ + return (unsigned)pix_fmt < AV_PIX_FMT_NB ? + format_entries[pix_fmt].is_supported_endianness : 0; +} #if FF_API_SWS_FORMAT_NAME const char *sws_format_name(enum AVPixelFormat format) @@ -236,11 +251,41 @@ static double getSplineCoeff(double a, double b, double c, double d, dist - 1.0); } -static int initFilter(int16_t **outFilter, int32_t **filterPos, - int *outFilterSize, int xInc, int srcW, int dstW, - int filterAlign, int one, int flags, int cpu_flags, - SwsVector *srcFilter, SwsVector *dstFilter, - double param[2]) +static av_cold int get_local_pos(SwsContext *s, int chr_subsample, int pos, int dir) +{ + if (pos < 0) { + pos = (128 << chr_subsample) - 128; + } + pos += 128; // relative to ideal left edge + return pos >> chr_subsample; +} + +typedef struct { + int flag; ///< flag associated to the algorithm + const char *description; ///< human-readable description + int size_factor; ///< size factor used when initing the filters +} ScaleAlgorithm; + +static const ScaleAlgorithm scale_algorithms[] = { + { SWS_AREA, "area averaging", 1 /* downscale only, for upscale it is bilinear */ }, + { SWS_BICUBIC, "bicubic", 4 }, + { SWS_BICUBLIN, "luma bicubic / chroma bilinear", -1 }, + { SWS_BILINEAR, "bilinear", 2 }, + { SWS_FAST_BILINEAR, "fast bilinear", -1 }, + { SWS_GAUSS, "Gaussian", 8 /* infinite ;) */ }, + { SWS_LANCZOS, "Lanczos", -1 /* custom */ }, + { SWS_POINT, "nearest neighbor / point", -1 }, + { SWS_SINC, "sinc", 20 /* infinite ;) */ }, + { SWS_SPLINE, "bicubic spline", 20 /* infinite :)*/ }, + { SWS_X, "experimental", 8 }, +}; + +static av_cold int initFilter(int16_t **outFilter, int32_t **filterPos, + int *outFilterSize, int xInc, int srcW, + int dstW, int filterAlign, int one, + int flags, int cpu_flags, + SwsVector *srcFilter, SwsVector *dstFilter, + double param[2], int srcPos, int dstPos) { int i; int filterSize; @@ -256,7 +301,7 @@ static int initFilter(int16_t **outFilter, int32_t **filterPos, // NOTE: the +3 is for the MMX(+1) / SSE(+3) scaler which reads over the end FF_ALLOC_OR_GOTO(NULL, *filterPos, (dstW + 3) * sizeof(**filterPos), fail); - if (FFABS(xInc - 0x10000) < 10) { // unscaled + if (FFABS(xInc - 0x10000) < 10 && srcPos == dstPos) { // unscaled int i; filterSize = 1; FF_ALLOCZ_OR_GOTO(NULL, filter, @@ -273,7 +318,7 @@ static int initFilter(int16_t **outFilter, int32_t **filterPos, FF_ALLOC_OR_GOTO(NULL, filter, dstW * sizeof(*filter) * filterSize, fail); - xDstInSrc = xInc / 2 - 0x8000; + xDstInSrc = ((dstPos*(int64_t)xInc)>>8) - ((srcPos*0x8000LL)>>7); for (i = 0; i < dstW; i++) { int xx = (xDstInSrc - ((filterSize - 1) << 15) + (1 << 15)) >> 16; @@ -289,7 +334,7 @@ static int initFilter(int16_t **outFilter, int32_t **filterPos, FF_ALLOC_OR_GOTO(NULL, filter, dstW * sizeof(*filter) * filterSize, fail); - xDstInSrc = xInc / 2 - 0x8000; + xDstInSrc = ((dstPos*(int64_t)xInc)>>8) - ((srcPos*0x8000LL)>>7); for (i = 0; i < dstW; i++) { int xx = (xDstInSrc - ((filterSize - 1) << 15) + (1 << 15)) >> 16; int j; @@ -307,27 +352,17 @@ static int initFilter(int16_t **outFilter, int32_t **filterPos, } } else { int64_t xDstInSrc; - int sizeFactor; - - if (flags & SWS_BICUBIC) - sizeFactor = 4; - else if (flags & SWS_X) - sizeFactor = 8; - else if (flags & SWS_AREA) - sizeFactor = 1; // downscale only, for upscale it is bilinear - else if (flags & SWS_GAUSS) - sizeFactor = 8; // infinite ;) - else if (flags & SWS_LANCZOS) - sizeFactor = param[0] != SWS_PARAM_DEFAULT ? ceil(2 * param[0]) : 6; - else if (flags & SWS_SINC) - sizeFactor = 20; // infinite ;) - else if (flags & SWS_SPLINE) - sizeFactor = 20; // infinite ;) - else if (flags & SWS_BILINEAR) - sizeFactor = 2; - else { - av_assert0(0); + int sizeFactor = -1; + + for (i = 0; i < FF_ARRAY_ELEMS(scale_algorithms); i++) { + if (flags & scale_algorithms[i].flag) { + sizeFactor = scale_algorithms[i].size_factor; + break; + } } + if (flags & SWS_LANCZOS) + sizeFactor = param[0] != SWS_PARAM_DEFAULT ? ceil(2 * param[0]) : 6; + av_assert0(sizeFactor > 0); if (xInc <= 1 << 16) filterSize = 1 + sizeFactor; // upscale @@ -340,7 +375,7 @@ static int initFilter(int16_t **outFilter, int32_t **filterPos, FF_ALLOC_OR_GOTO(NULL, filter, dstW * sizeof(*filter) * filterSize, fail); - xDstInSrc = xInc - 0x10000; + xDstInSrc = ((dstPos*(int64_t)xInc)>>7) - ((srcPos*0x10000LL)>>7); for (i = 0; i < dstW; i++) { int xx = (xDstInSrc - ((filterSize - 2) << 16)) / (1 << 17); int j; @@ -508,7 +543,7 @@ static int initFilter(int16_t **outFilter, int32_t **filterPos, minFilterSize = min; } - if (HAVE_ALTIVEC && cpu_flags & AV_CPU_FLAG_ALTIVEC) { + if (PPC_ALTIVEC(cpu_flags)) { // we can handle the special case 4, so we don't want to go the full 8 if (minFilterSize < 5) filterAlign = 4; @@ -520,7 +555,7 @@ static int initFilter(int16_t **outFilter, int32_t **filterPos, filterAlign = 1; } - if (INLINE_MMX(cpu_flags)) { + if (HAVE_MMX && cpu_flags & AV_CPU_FLAG_MMX) { // special case for unscaled vertical filtering if (minFilterSize == 1 && filterAlign == 2) filterAlign = 1; @@ -532,7 +567,7 @@ static int initFilter(int16_t **outFilter, int32_t **filterPos, filter = av_malloc(filterSize * dstW * sizeof(*filter)); if (filterSize >= MAX_FILTER_SIZE * 16 / ((flags & SWS_ACCURATE_RND) ? APCK_SIZE : 16) || !filter) { - av_log(NULL, AV_LOG_ERROR, "sws: filterSize %d is too large, try less extreem scaling or increase MAX_FILTER_SIZE and recompile\n", filterSize); + av_log(NULL, AV_LOG_ERROR, "sws: filterSize %d is too large, try less extreme scaling or increase MAX_FILTER_SIZE and recompile\n", filterSize); goto fail; } *outFilterSize = filterSize; @@ -597,6 +632,10 @@ static int initFilter(int16_t **outFilter, int32_t **filterPos, sum += filter[i * filterSize + j]; } sum = (sum + one / 2) / one; + if (!sum) { + av_log(NULL, AV_LOG_WARNING, "SwScaler: zero vector in scaling\n"); + sum = 1; + } for (j = 0; j < *outFilterSize; j++) { int64_t v = filter[i * filterSize + j] + error; int intV = ROUNDED_DIV(v, sum); @@ -627,9 +666,9 @@ fail: } #if HAVE_MMXEXT_INLINE -static int init_hscaler_mmxext(int dstW, int xInc, uint8_t *filterCode, - int16_t *filter, int32_t *filterPos, - int numSplits) +static av_cold int init_hscaler_mmxext(int dstW, int xInc, uint8_t *filterCode, + int16_t *filter, int32_t *filterPos, + int numSplits) { uint8_t *fragmentA; x86_reg imm8OfPShufW1A; @@ -742,10 +781,10 @@ static int init_hscaler_mmxext(int dstW, int xInc, uint8_t *filterCode, int c = ((xpos + xInc * 2) >> 16) - xx; int d = ((xpos + xInc * 3) >> 16) - xx; int inc = (d + 1 < 4); - uint8_t *fragment = (d + 1 < 4) ? fragmentB : fragmentA; - x86_reg imm8OfPShufW1 = (d + 1 < 4) ? imm8OfPShufW1B : imm8OfPShufW1A; - x86_reg imm8OfPShufW2 = (d + 1 < 4) ? imm8OfPShufW2B : imm8OfPShufW2A; - x86_reg fragmentLength = (d + 1 < 4) ? fragmentLengthB : fragmentLengthA; + uint8_t *fragment = inc ? fragmentB : fragmentA; + x86_reg imm8OfPShufW1 = inc ? imm8OfPShufW1B : imm8OfPShufW1A; + x86_reg imm8OfPShufW2 = inc ? imm8OfPShufW2B : imm8OfPShufW2A; + x86_reg fragmentLength = inc ? fragmentLengthB : fragmentLengthA; int maxShift = 3 - (d + inc); int shift = 0; @@ -792,21 +831,148 @@ static int init_hscaler_mmxext(int dstW, int xInc, uint8_t *filterCode, } #endif /* HAVE_MMXEXT_INLINE */ -static void getSubSampleFactors(int *h, int *v, enum AVPixelFormat format) +static void fill_rgb2yuv_table(SwsContext *c, const int table[4], int dstRange) { - const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(format); - *h = desc->log2_chroma_w; - *v = desc->log2_chroma_h; + int64_t W, V, Z, Cy, Cu, Cv; + int64_t vr = table[0]; + int64_t ub = table[1]; + int64_t ug = -table[2]; + int64_t vg = -table[3]; + int64_t ONE = 65536; + int64_t cy = ONE; + uint8_t *p = (uint8_t*)c->input_rgb2yuv_table; + int i; + static const int8_t map[] = { + BY_IDX, GY_IDX, -1 , BY_IDX, BY_IDX, GY_IDX, -1 , BY_IDX, + RY_IDX, -1 , GY_IDX, RY_IDX, RY_IDX, -1 , GY_IDX, RY_IDX, + RY_IDX, GY_IDX, -1 , RY_IDX, RY_IDX, GY_IDX, -1 , RY_IDX, + BY_IDX, -1 , GY_IDX, BY_IDX, BY_IDX, -1 , GY_IDX, BY_IDX, + BU_IDX, GU_IDX, -1 , BU_IDX, BU_IDX, GU_IDX, -1 , BU_IDX, + RU_IDX, -1 , GU_IDX, RU_IDX, RU_IDX, -1 , GU_IDX, RU_IDX, + RU_IDX, GU_IDX, -1 , RU_IDX, RU_IDX, GU_IDX, -1 , RU_IDX, + BU_IDX, -1 , GU_IDX, BU_IDX, BU_IDX, -1 , GU_IDX, BU_IDX, + BV_IDX, GV_IDX, -1 , BV_IDX, BV_IDX, GV_IDX, -1 , BV_IDX, + RV_IDX, -1 , GV_IDX, RV_IDX, RV_IDX, -1 , GV_IDX, RV_IDX, + RV_IDX, GV_IDX, -1 , RV_IDX, RV_IDX, GV_IDX, -1 , RV_IDX, + BV_IDX, -1 , GV_IDX, BV_IDX, BV_IDX, -1 , GV_IDX, BV_IDX, + RY_IDX, BY_IDX, RY_IDX, BY_IDX, RY_IDX, BY_IDX, RY_IDX, BY_IDX, + BY_IDX, RY_IDX, BY_IDX, RY_IDX, BY_IDX, RY_IDX, BY_IDX, RY_IDX, + GY_IDX, -1 , GY_IDX, -1 , GY_IDX, -1 , GY_IDX, -1 , + -1 , GY_IDX, -1 , GY_IDX, -1 , GY_IDX, -1 , GY_IDX, + RU_IDX, BU_IDX, RU_IDX, BU_IDX, RU_IDX, BU_IDX, RU_IDX, BU_IDX, + BU_IDX, RU_IDX, BU_IDX, RU_IDX, BU_IDX, RU_IDX, BU_IDX, RU_IDX, + GU_IDX, -1 , GU_IDX, -1 , GU_IDX, -1 , GU_IDX, -1 , + -1 , GU_IDX, -1 , GU_IDX, -1 , GU_IDX, -1 , GU_IDX, + RV_IDX, BV_IDX, RV_IDX, BV_IDX, RV_IDX, BV_IDX, RV_IDX, BV_IDX, + BV_IDX, RV_IDX, BV_IDX, RV_IDX, BV_IDX, RV_IDX, BV_IDX, RV_IDX, + GV_IDX, -1 , GV_IDX, -1 , GV_IDX, -1 , GV_IDX, -1 , + -1 , GV_IDX, -1 , GV_IDX, -1 , GV_IDX, -1 , GV_IDX, //23 + -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , //24 + -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , //25 + -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , //26 + -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , //27 + -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , //28 + -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , //29 + -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , //30 + -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , //31 + BY_IDX, GY_IDX, RY_IDX, -1 , -1 , -1 , -1 , -1 , //32 + BU_IDX, GU_IDX, RU_IDX, -1 , -1 , -1 , -1 , -1 , //33 + BV_IDX, GV_IDX, RV_IDX, -1 , -1 , -1 , -1 , -1 , //34 + }; + + dstRange = 0; //FIXME range = 1 is handled elsewhere + + if (!dstRange) { + cy = cy * 255 / 219; + } else { + vr = vr * 224 / 255; + ub = ub * 224 / 255; + ug = ug * 224 / 255; + vg = vg * 224 / 255; + } + W = ROUNDED_DIV(ONE*ONE*ug, ub); + V = ROUNDED_DIV(ONE*ONE*vg, vr); + Z = ONE*ONE-W-V; + + Cy = ROUNDED_DIV(cy*Z, ONE); + Cu = ROUNDED_DIV(ub*Z, ONE); + Cv = ROUNDED_DIV(vr*Z, ONE); + + c->input_rgb2yuv_table[RY_IDX] = -ROUNDED_DIV((1 << RGB2YUV_SHIFT)*V , Cy); + c->input_rgb2yuv_table[GY_IDX] = ROUNDED_DIV((1 << RGB2YUV_SHIFT)*ONE*ONE , Cy); + c->input_rgb2yuv_table[BY_IDX] = -ROUNDED_DIV((1 << RGB2YUV_SHIFT)*W , Cy); + + c->input_rgb2yuv_table[RU_IDX] = ROUNDED_DIV((1 << RGB2YUV_SHIFT)*V , Cu); + c->input_rgb2yuv_table[GU_IDX] = -ROUNDED_DIV((1 << RGB2YUV_SHIFT)*ONE*ONE , Cu); + c->input_rgb2yuv_table[BU_IDX] = ROUNDED_DIV((1 << RGB2YUV_SHIFT)*(Z+W) , Cu); + + c->input_rgb2yuv_table[RV_IDX] = ROUNDED_DIV((1 << RGB2YUV_SHIFT)*(V+Z) , Cv); + c->input_rgb2yuv_table[GV_IDX] = -ROUNDED_DIV((1 << RGB2YUV_SHIFT)*ONE*ONE , Cv); + c->input_rgb2yuv_table[BV_IDX] = ROUNDED_DIV((1 << RGB2YUV_SHIFT)*W , Cv); + + if(/*!dstRange && */!memcmp(table, ff_yuv2rgb_coeffs[SWS_CS_DEFAULT], sizeof(ff_yuv2rgb_coeffs[SWS_CS_DEFAULT]))) { + c->input_rgb2yuv_table[BY_IDX] = ((int)(0.114 * 219 / 255 * (1 << RGB2YUV_SHIFT) + 0.5)); + c->input_rgb2yuv_table[BV_IDX] = (-(int)(0.081 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5)); + c->input_rgb2yuv_table[BU_IDX] = ((int)(0.500 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5)); + c->input_rgb2yuv_table[GY_IDX] = ((int)(0.587 * 219 / 255 * (1 << RGB2YUV_SHIFT) + 0.5)); + c->input_rgb2yuv_table[GV_IDX] = (-(int)(0.419 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5)); + c->input_rgb2yuv_table[GU_IDX] = (-(int)(0.331 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5)); + c->input_rgb2yuv_table[RY_IDX] = ((int)(0.299 * 219 / 255 * (1 << RGB2YUV_SHIFT) + 0.5)); + c->input_rgb2yuv_table[RV_IDX] = ((int)(0.500 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5)); + c->input_rgb2yuv_table[RU_IDX] = (-(int)(0.169 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5)); + } + for(i=0; i<FF_ARRAY_ELEMS(map); i++) + AV_WL16(p + 16*4 + 2*i, map[i] >= 0 ? c->input_rgb2yuv_table[map[i]] : 0); +} + +static void fill_xyztables(struct SwsContext *c) +{ + int i; + double xyzgamma = XYZ_GAMMA; + double rgbgamma = 1.0 / RGB_GAMMA; + double xyzgammainv = 1.0 / XYZ_GAMMA; + double rgbgammainv = RGB_GAMMA; + static const int16_t xyz2rgb_matrix[3][4] = { + {13270, -6295, -2041}, + {-3969, 7682, 170}, + { 228, -835, 4329} }; + static const int16_t rgb2xyz_matrix[3][4] = { + {1689, 1464, 739}, + { 871, 2929, 296}, + { 79, 488, 3891} }; + static int16_t xyzgamma_tab[4096], rgbgamma_tab[4096], xyzgammainv_tab[4096], rgbgammainv_tab[4096]; + + memcpy(c->xyz2rgb_matrix, xyz2rgb_matrix, sizeof(c->xyz2rgb_matrix)); + memcpy(c->rgb2xyz_matrix, rgb2xyz_matrix, sizeof(c->rgb2xyz_matrix)); + c->xyzgamma = xyzgamma_tab; + c->rgbgamma = rgbgamma_tab; + c->xyzgammainv = xyzgammainv_tab; + c->rgbgammainv = rgbgammainv_tab; + + if (rgbgamma_tab[4095]) + return; + + /* set gamma vectors */ + for (i = 0; i < 4096; i++) { + xyzgamma_tab[i] = lrint(pow(i / 4095.0, xyzgamma) * 4095.0); + rgbgamma_tab[i] = lrint(pow(i / 4095.0, rgbgamma) * 4095.0); + xyzgammainv_tab[i] = lrint(pow(i / 4095.0, xyzgammainv) * 4095.0); + rgbgammainv_tab[i] = lrint(pow(i / 4095.0, rgbgammainv) * 4095.0); + } } int sws_setColorspaceDetails(struct SwsContext *c, const int inv_table[4], int srcRange, const int table[4], int dstRange, int brightness, int contrast, int saturation) { - const AVPixFmtDescriptor *desc_dst = av_pix_fmt_desc_get(c->dstFormat); - const AVPixFmtDescriptor *desc_src = av_pix_fmt_desc_get(c->srcFormat); - memcpy(c->srcColorspaceTable, inv_table, sizeof(int) * 4); - memcpy(c->dstColorspaceTable, table, sizeof(int) * 4); + const AVPixFmtDescriptor *desc_dst; + const AVPixFmtDescriptor *desc_src; + memmove(c->srcColorspaceTable, inv_table, sizeof(int) * 4); + memmove(c->dstColorspaceTable, table, sizeof(int) * 4); + + handle_formats(c); + desc_dst = av_pix_fmt_desc_get(c->dstFormat); + desc_src = av_pix_fmt_desc_get(c->srcFormat); if(!isYUV(c->dstFormat) && !isGray(c->dstFormat)) dstRange = 0; @@ -819,19 +985,24 @@ int sws_setColorspaceDetails(struct SwsContext *c, const int inv_table[4], c->srcRange = srcRange; c->dstRange = dstRange; - if (isYUV(c->dstFormat) || isGray(c->dstFormat)) + if ((isYUV(c->dstFormat) || isGray(c->dstFormat)) && (isYUV(c->srcFormat) || isGray(c->srcFormat))) return -1; c->dstFormatBpp = av_get_bits_per_pixel(desc_dst); c->srcFormatBpp = av_get_bits_per_pixel(desc_src); - ff_yuv2rgb_c_init_tables(c, inv_table, srcRange, brightness, - contrast, saturation); - // FIXME factorize + if (!isYUV(c->dstFormat) && !isGray(c->dstFormat)) { + ff_yuv2rgb_c_init_tables(c, inv_table, srcRange, brightness, + contrast, saturation); + // FIXME factorize - if (HAVE_ALTIVEC && av_get_cpu_flags() & AV_CPU_FLAG_ALTIVEC) - ff_yuv2rgb_init_tables_altivec(c, inv_table, brightness, + if (ARCH_PPC) + ff_yuv2rgb_init_tables_ppc(c, inv_table, brightness, contrast, saturation); + } + + fill_rgb2yuv_table(c, table, dstRange); + return 0; } @@ -839,7 +1010,7 @@ int sws_getColorspaceDetails(struct SwsContext *c, int **inv_table, int *srcRange, int **table, int *dstRange, int *brightness, int *contrast, int *saturation) { - if (!c || isYUV(c->dstFormat) || isGray(c->dstFormat)) + if (!c ) return -1; *inv_table = c->srcColorspaceTable; @@ -859,6 +1030,9 @@ static int handle_jpeg(enum AVPixelFormat *format) case AV_PIX_FMT_YUVJ420P: *format = AV_PIX_FMT_YUV420P; return 1; + case AV_PIX_FMT_YUVJ411P: + *format = AV_PIX_FMT_YUV411P; + return 1; case AV_PIX_FMT_YUVJ422P: *format = AV_PIX_FMT_YUV422P; return 1; @@ -868,6 +1042,8 @@ static int handle_jpeg(enum AVPixelFormat *format) case AV_PIX_FMT_YUVJ440P: *format = AV_PIX_FMT_YUV440P; return 1; + case AV_PIX_FMT_GRAY8: + return 1; default: return 0; } @@ -884,6 +1060,25 @@ static int handle_0alpha(enum AVPixelFormat *format) } } +static int handle_xyz(enum AVPixelFormat *format) +{ + switch (*format) { + case AV_PIX_FMT_XYZ12BE : *format = AV_PIX_FMT_RGB48BE; return 1; + case AV_PIX_FMT_XYZ12LE : *format = AV_PIX_FMT_RGB48LE; return 1; + default: return 0; + } +} + +static void handle_formats(SwsContext *c) +{ + c->src0Alpha |= handle_0alpha(&c->srcFormat); + c->dst0Alpha |= handle_0alpha(&c->dstFormat); + c->srcXYZ |= handle_xyz(&c->srcFormat); + c->dstXYZ |= handle_xyz(&c->dstFormat); + if (c->srcXYZ || c->dstXYZ) + fill_xyztables(c); +} + SwsContext *sws_alloc_context(void) { SwsContext *c = av_mallocz(sizeof(SwsContext)); @@ -911,8 +1106,8 @@ av_cold int sws_init_context(SwsContext *c, SwsFilter *srcFilter, int flags, cpu_flags; enum AVPixelFormat srcFormat = c->srcFormat; enum AVPixelFormat dstFormat = c->dstFormat; - const AVPixFmtDescriptor *desc_src = av_pix_fmt_desc_get(srcFormat); - const AVPixFmtDescriptor *desc_dst = av_pix_fmt_desc_get(dstFormat); + const AVPixFmtDescriptor *desc_src; + const AVPixFmtDescriptor *desc_dst; cpu_flags = av_get_cpu_flags(); flags = c->flags; @@ -922,17 +1117,25 @@ av_cold int sws_init_context(SwsContext *c, SwsFilter *srcFilter, unscaled = (srcW == dstW && srcH == dstH); - handle_jpeg(&srcFormat); - handle_jpeg(&dstFormat); - handle_0alpha(&srcFormat); - handle_0alpha(&dstFormat); + c->srcRange |= handle_jpeg(&c->srcFormat); + c->dstRange |= handle_jpeg(&c->dstFormat); - if(srcFormat!=c->srcFormat || dstFormat!=c->dstFormat){ + if(srcFormat!=c->srcFormat || dstFormat!=c->dstFormat) av_log(c, AV_LOG_WARNING, "deprecated pixel format used, make sure you did set range correctly\n"); - c->srcFormat= srcFormat; - c->dstFormat= dstFormat; - } + if (!c->contrast && !c->saturation && !c->dstFormatBpp) + sws_setColorspaceDetails(c, ff_yuv2rgb_coeffs[SWS_CS_DEFAULT], c->srcRange, + ff_yuv2rgb_coeffs[SWS_CS_DEFAULT], + c->dstRange, 0, 1 << 16, 1 << 16); + + handle_formats(c); + srcFormat = c->srcFormat; + dstFormat = c->dstFormat; + desc_src = av_pix_fmt_desc_get(srcFormat); + desc_dst = av_pix_fmt_desc_get(dstFormat); + + if (!(unscaled && sws_isSupportedEndiannessConversion(srcFormat) && + av_pix_fmt_swap_endianness(srcFormat) == dstFormat)) { if (!sws_isSupportedInput(srcFormat)) { av_log(c, AV_LOG_ERROR, "%s is not supported as input pixel format\n", av_get_pix_fmt_name(srcFormat)); @@ -943,6 +1146,7 @@ av_cold int sws_init_context(SwsContext *c, SwsFilter *srcFilter, av_get_pix_fmt_name(dstFormat)); return AVERROR(EINVAL); } + } i = flags & (SWS_POINT | SWS_AREA | @@ -955,8 +1159,19 @@ av_cold int sws_init_context(SwsContext *c, SwsFilter *srcFilter, SWS_SINC | SWS_SPLINE | SWS_BICUBLIN); - if (!i || (i & (i - 1))) { - av_log(c, AV_LOG_ERROR, "Exactly one scaler algorithm must be chosen, got %X\n", i); + + /* provide a default scaler if not set by caller */ + if (!i) { + if (dstW < srcW && dstH < srcH) + flags |= SWS_BICUBIC; + else if (dstW > srcW && dstH > srcH) + flags |= SWS_BICUBIC; + else + flags |= SWS_BICUBIC; + c->flags = flags; + } else if (i & (i - 1)) { + av_log(c, AV_LOG_ERROR, + "Exactly one scaler algorithm must be chosen, got %X\n", i); return AVERROR(EINVAL); } /* sanity check */ @@ -988,8 +1203,8 @@ av_cold int sws_init_context(SwsContext *c, SwsFilter *srcFilter, (dstFilter->lumH && dstFilter->lumH->length > 1) || (dstFilter->chrH && dstFilter->chrH->length > 1); - getSubSampleFactors(&c->chrSrcHSubSample, &c->chrSrcVSubSample, srcFormat); - getSubSampleFactors(&c->chrDstHSubSample, &c->chrDstVSubSample, dstFormat); + av_pix_fmt_get_chroma_sub_sample(srcFormat, &c->chrSrcHSubSample, &c->chrSrcVSubSample); + av_pix_fmt_get_chroma_sub_sample(dstFormat, &c->chrDstHSubSample, &c->chrDstVSubSample); if (isAnyRGB(dstFormat) && !(flags&SWS_FULL_CHR_H_INT)) { if (dstW&1) { @@ -997,25 +1212,45 @@ av_cold int sws_init_context(SwsContext *c, SwsFilter *srcFilter, flags |= SWS_FULL_CHR_H_INT; c->flags = flags; } + + if ( c->chrSrcHSubSample == 0 + && c->chrSrcVSubSample == 0 + && c->dither != SWS_DITHER_BAYER //SWS_FULL_CHR_H_INT is currently not supported with SWS_DITHER_BAYER + && !(c->flags & SWS_FAST_BILINEAR) + ) { + av_log(c, AV_LOG_DEBUG, "Forcing full internal H chroma due to input having non subsampled chroma\n"); + flags |= SWS_FULL_CHR_H_INT; + c->flags = flags; + } + } + + if (c->dither == SWS_DITHER_AUTO) { + if (flags & SWS_ERROR_DIFFUSION) + c->dither = SWS_DITHER_ED; } if(dstFormat == AV_PIX_FMT_BGR4_BYTE || dstFormat == AV_PIX_FMT_RGB4_BYTE || dstFormat == AV_PIX_FMT_BGR8 || dstFormat == AV_PIX_FMT_RGB8) { - if (flags & SWS_ERROR_DIFFUSION && !(flags & SWS_FULL_CHR_H_INT)) { - av_log(c, AV_LOG_DEBUG, - "Error diffusion dither is only supported in full chroma interpolation for destination format '%s'\n", - av_get_pix_fmt_name(dstFormat)); - flags |= SWS_FULL_CHR_H_INT; - c->flags = flags; + if (c->dither == SWS_DITHER_AUTO) + c->dither = (flags & SWS_FULL_CHR_H_INT) ? SWS_DITHER_ED : SWS_DITHER_BAYER; + if (!(flags & SWS_FULL_CHR_H_INT)) { + if (c->dither == SWS_DITHER_ED) { + av_log(c, AV_LOG_DEBUG, + "Desired dithering only supported in full chroma interpolation for destination format '%s'\n", + av_get_pix_fmt_name(dstFormat)); + flags |= SWS_FULL_CHR_H_INT; + c->flags = flags; + } } - if (!(flags & SWS_ERROR_DIFFUSION) && (flags & SWS_FULL_CHR_H_INT)) { - av_log(c, AV_LOG_DEBUG, - "Ordered dither is not supported in full chroma interpolation for destination format '%s'\n", - av_get_pix_fmt_name(dstFormat)); - flags |= SWS_ERROR_DIFFUSION; - c->flags = flags; + if (flags & SWS_FULL_CHR_H_INT) { + if (c->dither == SWS_DITHER_BAYER) { + av_log(c, AV_LOG_DEBUG, + "Ordered dither is not supported in full chroma interpolation for destination format '%s'\n", + av_get_pix_fmt_name(dstFormat)); + c->dither = SWS_DITHER_ED; + } } } if (isPlanarRGB(dstFormat)) { @@ -1073,11 +1308,11 @@ av_cold int sws_init_context(SwsContext *c, SwsFilter *srcFilter, (flags & SWS_FAST_BILINEAR))) c->chrSrcHSubSample = 1; - // Note the -((-x)>>y) is so that we always round toward +inf. - c->chrSrcW = -((-srcW) >> c->chrSrcHSubSample); - c->chrSrcH = -((-srcH) >> c->chrSrcVSubSample); - c->chrDstW = -((-dstW) >> c->chrDstHSubSample); - c->chrDstH = -((-dstH) >> c->chrDstVSubSample); + // Note the FF_CEIL_RSHIFT is so that we always round toward +inf. + c->chrSrcW = FF_CEIL_RSHIFT(srcW, c->chrSrcHSubSample); + c->chrSrcH = FF_CEIL_RSHIFT(srcH, c->chrSrcVSubSample); + c->chrDstW = FF_CEIL_RSHIFT(dstW, c->chrDstHSubSample); + c->chrDstH = FF_CEIL_RSHIFT(dstH, c->chrDstVSubSample); FF_ALLOC_OR_GOTO(c, c->formatConvBuffer, FFALIGN(srcW*2+78, 16) * 2, fail); @@ -1086,7 +1321,7 @@ av_cold int sws_init_context(SwsContext *c, SwsFilter *srcFilter, (c->srcRange == c->dstRange || isAnyRGB(dstFormat))) { ff_get_unscaled_swscale(c); - if (c->swScale) { + if (c->swscale) { if (flags & SWS_PRINT_INFO) av_log(c, AV_LOG_INFO, "using unscaled %s -> %s special converter\n", @@ -1107,9 +1342,10 @@ av_cold int sws_init_context(SwsContext *c, SwsFilter *srcFilter, dst_stride <<= 1; if (INLINE_MMXEXT(cpu_flags) && c->srcBpc == 8 && c->dstBpc <= 14) { - c->canMMXEXTBeUsed = (dstW >= srcW && (dstW & 31) == 0 && - (srcW & 15) == 0) ? 1 : 0; - if (!c->canMMXEXTBeUsed && dstW >= srcW && (srcW & 15) == 0 + c->canMMXEXTBeUsed = dstW >= srcW && (dstW & 31) == 0 && + c->chrDstW >= c->chrSrcW && + (srcW & 15) == 0; + if (!c->canMMXEXTBeUsed && dstW >= srcW && c->chrDstW >= c->chrSrcW && (srcW & 15) == 0 && (flags & SWS_FAST_BILINEAR)) { if (flags & SWS_PRINT_INFO) @@ -1199,53 +1435,61 @@ av_cold int sws_init_context(SwsContext *c, SwsFilter *srcFilter, c->hChrFilter, (uint32_t*)c->hChrFilterPos, 4); #if USE_MMAP - mprotect(c->lumMmxextFilterCode, c->lumMmxextFilterCodeSize, PROT_EXEC | PROT_READ); - mprotect(c->chrMmxextFilterCode, c->chrMmxextFilterCodeSize, PROT_EXEC | PROT_READ); + if ( mprotect(c->lumMmxextFilterCode, c->lumMmxextFilterCodeSize, PROT_EXEC | PROT_READ) == -1 + || mprotect(c->chrMmxextFilterCode, c->chrMmxextFilterCodeSize, PROT_EXEC | PROT_READ) == -1) { + av_log(c, AV_LOG_ERROR, "mprotect failed, cannot use fast bilinear scaler\n"); + goto fail; + } #endif } else #endif /* HAVE_MMXEXT_INLINE */ { - const int filterAlign = - (HAVE_MMX && cpu_flags & AV_CPU_FLAG_MMX) ? 4 : - (HAVE_ALTIVEC && cpu_flags & AV_CPU_FLAG_ALTIVEC) ? 8 : - 1; + const int filterAlign = X86_MMX(cpu_flags) ? 4 : + PPC_ALTIVEC(cpu_flags) ? 8 : 1; if (initFilter(&c->hLumFilter, &c->hLumFilterPos, &c->hLumFilterSize, c->lumXInc, srcW, dstW, filterAlign, 1 << 14, (flags & SWS_BICUBLIN) ? (flags | SWS_BICUBIC) : flags, cpu_flags, srcFilter->lumH, dstFilter->lumH, - c->param) < 0) + c->param, + get_local_pos(c, 0, 0, 0), + get_local_pos(c, 0, 0, 0)) < 0) goto fail; if (initFilter(&c->hChrFilter, &c->hChrFilterPos, &c->hChrFilterSize, c->chrXInc, c->chrSrcW, c->chrDstW, filterAlign, 1 << 14, (flags & SWS_BICUBLIN) ? (flags | SWS_BILINEAR) : flags, cpu_flags, srcFilter->chrH, dstFilter->chrH, - c->param) < 0) + c->param, + get_local_pos(c, c->chrSrcHSubSample, c->src_h_chr_pos, 0), + get_local_pos(c, c->chrDstHSubSample, c->dst_h_chr_pos, 0)) < 0) goto fail; } } // initialize horizontal stuff /* precalculate vertical scaler filter coefficients */ { - const int filterAlign = - (HAVE_MMX && cpu_flags & AV_CPU_FLAG_MMX) ? 2 : - (HAVE_ALTIVEC && cpu_flags & AV_CPU_FLAG_ALTIVEC) ? 8 : - 1; + const int filterAlign = X86_MMX(cpu_flags) ? 2 : + PPC_ALTIVEC(cpu_flags) ? 8 : 1; if (initFilter(&c->vLumFilter, &c->vLumFilterPos, &c->vLumFilterSize, c->lumYInc, srcH, dstH, filterAlign, (1 << 12), (flags & SWS_BICUBLIN) ? (flags | SWS_BICUBIC) : flags, cpu_flags, srcFilter->lumV, dstFilter->lumV, - c->param) < 0) + c->param, + get_local_pos(c, 0, 0, 1), + get_local_pos(c, 0, 0, 1)) < 0) goto fail; if (initFilter(&c->vChrFilter, &c->vChrFilterPos, &c->vChrFilterSize, c->chrYInc, c->chrSrcH, c->chrDstH, filterAlign, (1 << 12), (flags & SWS_BICUBLIN) ? (flags | SWS_BILINEAR) : flags, cpu_flags, srcFilter->chrV, dstFilter->chrV, - c->param) < 0) + c->param, + get_local_pos(c, c->chrSrcVSubSample, c->src_v_chr_pos, 1), + get_local_pos(c, c->chrDstVSubSample, c->dst_v_chr_pos, 1)) < 0) + goto fail; #if HAVE_ALTIVEC @@ -1335,32 +1579,18 @@ av_cold int sws_init_context(SwsContext *c, SwsFilter *srcFilter, av_assert0(c->chrDstH <= dstH); if (flags & SWS_PRINT_INFO) { - if (flags & SWS_FAST_BILINEAR) - av_log(c, AV_LOG_INFO, "FAST_BILINEAR scaler, "); - else if (flags & SWS_BILINEAR) - av_log(c, AV_LOG_INFO, "BILINEAR scaler, "); - else if (flags & SWS_BICUBIC) - av_log(c, AV_LOG_INFO, "BICUBIC scaler, "); - else if (flags & SWS_X) - av_log(c, AV_LOG_INFO, "Experimental scaler, "); - else if (flags & SWS_POINT) - av_log(c, AV_LOG_INFO, "Nearest Neighbor / POINT scaler, "); - else if (flags & SWS_AREA) - av_log(c, AV_LOG_INFO, "Area Averaging scaler, "); - else if (flags & SWS_BICUBLIN) - av_log(c, AV_LOG_INFO, "luma BICUBIC / chroma BILINEAR scaler, "); - else if (flags & SWS_GAUSS) - av_log(c, AV_LOG_INFO, "Gaussian scaler, "); - else if (flags & SWS_SINC) - av_log(c, AV_LOG_INFO, "Sinc scaler, "); - else if (flags & SWS_LANCZOS) - av_log(c, AV_LOG_INFO, "Lanczos scaler, "); - else if (flags & SWS_SPLINE) - av_log(c, AV_LOG_INFO, "Bicubic spline scaler, "); - else - av_log(c, AV_LOG_INFO, "ehh flags invalid?! "); + const char *scaler = NULL, *cpucaps; - av_log(c, AV_LOG_INFO, "from %s to %s%s ", + for (i = 0; i < FF_ARRAY_ELEMS(scale_algorithms); i++) { + if (flags & scale_algorithms[i].flag) { + scaler = scale_algorithms[i].description; + break; + } + } + if (!scaler) + scaler = "ehh flags invalid?!"; + av_log(c, AV_LOG_INFO, "%s scaler, from %s to %s%s ", + scaler, av_get_pix_fmt_name(srcFormat), #ifdef DITHER1XBPP dstFormat == AV_PIX_FMT_BGR555 || dstFormat == AV_PIX_FMT_BGR565 || @@ -1373,15 +1603,17 @@ av_cold int sws_init_context(SwsContext *c, SwsFilter *srcFilter, av_get_pix_fmt_name(dstFormat)); if (INLINE_MMXEXT(cpu_flags)) - av_log(c, AV_LOG_INFO, "using MMXEXT\n"); + cpucaps = "MMXEXT"; else if (INLINE_AMD3DNOW(cpu_flags)) - av_log(c, AV_LOG_INFO, "using 3DNOW\n"); + cpucaps = "3DNOW"; else if (INLINE_MMX(cpu_flags)) - av_log(c, AV_LOG_INFO, "using MMX\n"); - else if (HAVE_ALTIVEC && cpu_flags & AV_CPU_FLAG_ALTIVEC) - av_log(c, AV_LOG_INFO, "using AltiVec\n"); + cpucaps = "MMX"; + else if (PPC_ALTIVEC(cpu_flags)) + cpucaps = "AltiVec"; else - av_log(c, AV_LOG_INFO, "using C\n"); + cpucaps = "C"; + + av_log(c, AV_LOG_INFO, "using %s\n", cpucaps); av_log(c, AV_LOG_VERBOSE, "%dx%d -> %dx%d\n", srcW, srcH, dstW, dstH); av_log(c, AV_LOG_DEBUG, @@ -1393,7 +1625,7 @@ av_cold int sws_init_context(SwsContext *c, SwsFilter *srcFilter, c->chrXInc, c->chrYInc); } - c->swScale = ff_getSwsFunc(c); + c->swscale = ff_getSwsFunc(c); return 0; fail: // FIXME replace things by appropriate error codes return -1; @@ -1415,10 +1647,6 @@ SwsContext *sws_getContext(int srcW, int srcH, enum AVPixelFormat srcFormat, c->srcH = srcH; c->dstW = dstW; c->dstH = dstH; - c->srcRange = handle_jpeg(&srcFormat); - c->dstRange = handle_jpeg(&dstFormat); - c->src0Alpha = handle_0alpha(&srcFormat); - c->dst0Alpha = handle_0alpha(&dstFormat); c->srcFormat = srcFormat; c->dstFormat = dstFormat; @@ -1426,9 +1654,6 @@ SwsContext *sws_getContext(int srcW, int srcH, enum AVPixelFormat srcFormat, c->param[0] = param[0]; c->param[1] = param[1]; } - sws_setColorspaceDetails(c, ff_yuv2rgb_coeffs[SWS_CS_DEFAULT], c->srcRange, - ff_yuv2rgb_coeffs[SWS_CS_DEFAULT] /* FIXME*/, - c->dstRange, 0, 1 << 16, 1 << 16); if (sws_init_context(c, srcFilter, dstFilter) < 0) { sws_freeContext(c); @@ -1695,14 +1920,12 @@ void sws_convVec(SwsVector *a, SwsVector *b) SwsVector *sws_cloneVec(SwsVector *a) { - int i; SwsVector *vec = sws_allocVec(a->length); if (!vec) return NULL; - for (i = 0; i < a->length; i++) - vec->coeff[i] = a->coeff[i]; + memcpy(vec->coeff, a->coeff, a->length * sizeof(*a->coeff)); return vec; } @@ -1747,14 +1970,10 @@ void sws_freeFilter(SwsFilter *filter) if (!filter) return; - if (filter->lumH) - sws_freeVec(filter->lumH); - if (filter->lumV) - sws_freeVec(filter->lumV); - if (filter->chrH) - sws_freeVec(filter->chrH); - if (filter->chrV) - sws_freeVec(filter->chrV); + sws_freeVec(filter->lumH); + sws_freeVec(filter->lumV); + sws_freeVec(filter->chrH); + sws_freeVec(filter->chrV); av_free(filter); } @@ -1858,21 +2077,13 @@ struct SwsContext *sws_getCachedContext(struct SwsContext *context, int srcW, return NULL; context->srcW = srcW; context->srcH = srcH; - context->srcRange = handle_jpeg(&srcFormat); - context->src0Alpha = handle_0alpha(&srcFormat); context->srcFormat = srcFormat; context->dstW = dstW; context->dstH = dstH; - context->dstRange = handle_jpeg(&dstFormat); - context->dst0Alpha = handle_0alpha(&dstFormat); context->dstFormat = dstFormat; context->flags = flags; context->param[0] = param[0]; context->param[1] = param[1]; - sws_setColorspaceDetails(context, ff_yuv2rgb_coeffs[SWS_CS_DEFAULT], - context->srcRange, - ff_yuv2rgb_coeffs[SWS_CS_DEFAULT] /* FIXME*/, - context->dstRange, 0, 1 << 16, 1 << 16); if (sws_init_context(context, srcFilter, dstFilter) < 0) { sws_freeContext(context); return NULL; |
