diff options
author | Paul B Mahol <onemda@gmail.com> | 2020-08-02 15:55:38 +0200 |
---|---|---|
committer | Paul B Mahol <onemda@gmail.com> | 2020-08-08 12:03:42 +0200 |
commit | 9d58cdb4ba9c2c53da74d0898e060cff30c74dcd (patch) | |
tree | da78bd7ef75beff53d0f051945fe87f6786888a7 /libswscale | |
parent | b9ff25a93e9bd7002b18b2bdca6b6a77d7ceb6a0 (diff) | |
download | ffmpeg-9d58cdb4ba9c2c53da74d0898e060cff30c74dcd.tar.gz |
swscale: do not drop half of bits from 16bit bayer formats
Diffstat (limited to 'libswscale')
-rw-r--r-- | libswscale/bayer_template.c | 102 | ||||
-rw-r--r-- | libswscale/swscale_internal.h | 7 | ||||
-rw-r--r-- | libswscale/swscale_unscaled.c | 51 | ||||
-rw-r--r-- | libswscale/utils.c | 5 |
4 files changed, 163 insertions, 2 deletions
diff --git a/libswscale/bayer_template.c b/libswscale/bayer_template.c index 1af1b6056e..46b5a4984d 100644 --- a/libswscale/bayer_template.c +++ b/libswscale/bayer_template.c @@ -118,6 +118,72 @@ B(1, 1) = (T(0, 1) + T(2, 1)) >> (1 + BAYER_SHIFT); #endif +#if defined(BAYER_BGGR) || defined(BAYER_RGGB) +#define BAYER_TO_RGB48_COPY \ + R(0, 0) = \ + R(0, 1) = \ + R(1, 1) = \ + R(1, 0) = S(1, 1); \ + \ + G(0, 1) = S(0, 1); \ + G(0, 0) = \ + G(1, 1) = (T(0, 1) + T(1, 0)) >> 1; \ + G(1, 0) = S(1, 0); \ + \ + B(1, 1) = \ + B(0, 0) = \ + B(0, 1) = \ + B(1, 0) = S(0, 0); +#define BAYER_TO_RGB48_INTERPOLATE \ + R(0, 0) = (T(-1, -1) + T(-1, 1) + T(1, -1) + T(1, 1)) >> 2; \ + G(0, 0) = (T(-1, 0) + T( 0, -1) + T(0, 1) + T(1, 0)) >> 2; \ + B(0, 0) = S(0, 0); \ + \ + R(0, 1) = (T(-1, 1) + T(1, 1)) >> 1; \ + G(0, 1) = S(0, 1); \ + B(0, 1) = (T(0, 0) + T(0, 2)) >> 1; \ + \ + R(1, 0) = (T(1, -1) + T(1, 1)) >> 1; \ + G(1, 0) = S(1, 0); \ + B(1, 0) = (T(0, 0) + T(2, 0)) >> 1; \ + \ + R(1, 1) = S(1, 1); \ + G(1, 1) = (T(0, 1) + T(1, 0) + T(1, 2) + T(2, 1)) >> 2; \ + B(1, 1) = (T(0, 0) + T(0, 2) + T(2, 0) + T(2, 2)) >> 2; +#else +#define BAYER_TO_RGB48_COPY \ + R(0, 0) = \ + R(0, 1) = \ + R(1, 1) = \ + R(1, 0) = S(1, 0); \ + \ + G(0, 0) = S(0, 0); \ + G(1, 1) = S(1, 1); \ + G(0, 1) = \ + G(1, 0) = (T(0, 0) + T(1, 1)) >> 1; \ + \ + B(1, 1) = \ + B(0, 0) = \ + B(0, 1) = \ + B(1, 0) = S(0, 1); +#define BAYER_TO_RGB48_INTERPOLATE \ + R(0, 0) = (T(-1, 0) + T(1, 0)) >> 1; \ + G(0, 0) = S(0, 0); \ + B(0, 0) = (T(0, -1) + T(0, 1)) >> 1; \ + \ + R(0, 1) = (T(-1, 0) + T(-1, 2) + T(1, 0) + T(1, 2)) >> 2; \ + G(0, 1) = (T(-1, 1) + T(0, 0) + T(0, 2) + T(1, 1)) >> 2; \ + B(0, 1) = S(0, 1); \ + \ + R(1, 0) = S(1, 0); \ + G(1, 0) = (T(0, 0) + T(1, -1) + T(1, 1) + T(2, 0)) >> 2; \ + B(1, 0) = (T(0, -1) + T(0, 1) + T(2, -1) + T(2, 1)) >> 2; \ + \ + R(1, 1) = (T(1, 0) + T(1, 2)) >> 1; \ + G(1, 1) = S(1, 1); \ + B(1, 1) = (T(0, 1) + T(2, 1)) >> 1; +#endif + /** * invoke ff_rgb24toyv12 for 2x2 pixels */ @@ -153,6 +219,40 @@ static void BAYER_RENAME(rgb24_interpolate)(const uint8_t *src, int src_stride, } } +static void BAYER_RENAME(rgb48_copy)(const uint8_t *src, int src_stride, uint8_t *ddst, int dst_stride, int width) +{ + uint16_t *dst = (uint16_t *)ddst; + int i; + + dst_stride /= 2; + for (i = 0 ; i < width; i+= 2) { + BAYER_TO_RGB48_COPY + src += 2 * BAYER_SIZEOF; + dst += 6; + } +} + +static void BAYER_RENAME(rgb48_interpolate)(const uint8_t *src, int src_stride, uint8_t *ddst, int dst_stride, int width) +{ + uint16_t *dst = (uint16_t *)ddst; + int i; + + dst_stride /= 2; + BAYER_TO_RGB48_COPY + src += 2 * BAYER_SIZEOF; + dst += 6; + + for (i = 2 ; i < width - 2; i+= 2) { + BAYER_TO_RGB48_INTERPOLATE + src += 2 * BAYER_SIZEOF; + dst += 6; + } + + if (width > 2) { + BAYER_TO_RGB48_COPY + } +} + static void BAYER_RENAME(yv12_copy)(const uint8_t *src, int src_stride, uint8_t *dstY, uint8_t *dstU, uint8_t *dstV, int luma_stride, int width, int32_t *rgb2yuv) { uint8_t dst[12]; @@ -203,6 +303,8 @@ static void BAYER_RENAME(yv12_interpolate)(const uint8_t *src, int src_stride, u #undef B #undef BAYER_TO_RGB24_COPY #undef BAYER_TO_RGB24_INTERPOLATE +#undef BAYER_TO_RGB48_COPY +#undef BAYER_TO_RGB48_INTERPOLATE #undef BAYER_RENAME diff --git a/libswscale/swscale_internal.h b/libswscale/swscale_internal.h index 1a1b6f0dee..d207d3beff 100644 --- a/libswscale/swscale_internal.h +++ b/libswscale/swscale_internal.h @@ -764,6 +764,13 @@ static av_always_inline int isBayer(enum AVPixelFormat pix_fmt) return !!(desc->flags & AV_PIX_FMT_FLAG_BAYER); } +static av_always_inline int isBayer16BPS(enum AVPixelFormat pix_fmt) +{ + const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt); + av_assert0(desc); + return desc->comp[1].depth == 8; +} + static av_always_inline int isAnyRGB(enum AVPixelFormat pix_fmt) { const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt); diff --git a/libswscale/swscale_unscaled.c b/libswscale/swscale_unscaled.c index 5fb572b51a..563de39696 100644 --- a/libswscale/swscale_unscaled.c +++ b/libswscale/swscale_unscaled.c @@ -1326,6 +1326,55 @@ static int bayer_to_rgb24_wrapper(SwsContext *c, const uint8_t* src[], int srcSt return srcSliceH; } +static int bayer_to_rgb48_wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY, + int srcSliceH, uint8_t* dst[], int dstStride[]) +{ + uint8_t *dstPtr= dst[0] + srcSliceY * dstStride[0]; + const uint8_t *srcPtr= src[0]; + int i; + void (*copy) (const uint8_t *src, int src_stride, uint8_t *dst, int dst_stride, int width); + void (*interpolate)(const uint8_t *src, int src_stride, uint8_t *dst, int dst_stride, int width); + + switch(c->srcFormat) { +#define CASE(pixfmt, prefix) \ + case pixfmt: copy = bayer_##prefix##_to_rgb48_copy; \ + interpolate = bayer_##prefix##_to_rgb48_interpolate; \ + break; + CASE(AV_PIX_FMT_BAYER_BGGR8, bggr8) + CASE(AV_PIX_FMT_BAYER_BGGR16LE, bggr16le) + CASE(AV_PIX_FMT_BAYER_BGGR16BE, bggr16be) + CASE(AV_PIX_FMT_BAYER_RGGB8, rggb8) + CASE(AV_PIX_FMT_BAYER_RGGB16LE, rggb16le) + CASE(AV_PIX_FMT_BAYER_RGGB16BE, rggb16be) + CASE(AV_PIX_FMT_BAYER_GBRG8, gbrg8) + CASE(AV_PIX_FMT_BAYER_GBRG16LE, gbrg16le) + CASE(AV_PIX_FMT_BAYER_GBRG16BE, gbrg16be) + CASE(AV_PIX_FMT_BAYER_GRBG8, grbg8) + CASE(AV_PIX_FMT_BAYER_GRBG16LE, grbg16le) + CASE(AV_PIX_FMT_BAYER_GRBG16BE, grbg16be) +#undef CASE + default: return 0; + } + + av_assert0(srcSliceH > 1); + + copy(srcPtr, srcStride[0], dstPtr, dstStride[0], c->srcW); + srcPtr += 2 * srcStride[0]; + dstPtr += 2 * dstStride[0]; + + for (i = 2; i < srcSliceH - 2; i += 2) { + interpolate(srcPtr, srcStride[0], dstPtr, dstStride[0], c->srcW); + srcPtr += 2 * srcStride[0]; + dstPtr += 2 * dstStride[0]; + } + + if (i + 1 == srcSliceH) { + copy(srcPtr, -srcStride[0], dstPtr, -dstStride[0], c->srcW); + } else if (i < srcSliceH) + copy(srcPtr, srcStride[0], dstPtr, dstStride[0], c->srcW); + return srcSliceH; +} + static int bayer_to_yv12_wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY, int srcSliceH, uint8_t* dst[], int dstStride[]) { @@ -2047,6 +2096,8 @@ void ff_get_unscaled_swscale(SwsContext *c) if (isBayer(srcFormat)) { if (dstFormat == AV_PIX_FMT_RGB24) c->swscale = bayer_to_rgb24_wrapper; + else if (dstFormat == AV_PIX_FMT_RGB48) + c->swscale = bayer_to_rgb48_wrapper; else if (dstFormat == AV_PIX_FMT_YUV420P) c->swscale = bayer_to_yv12_wrapper; else if (!isBayer(dstFormat)) { diff --git a/libswscale/utils.c b/libswscale/utils.c index dcd1dbaa76..9ca378bd3b 100644 --- a/libswscale/utils.c +++ b/libswscale/utils.c @@ -1536,8 +1536,9 @@ av_cold int sws_init_context(SwsContext *c, SwsFilter *srcFilter, if (isBayer(srcFormat)) { if (!unscaled || - (dstFormat != AV_PIX_FMT_RGB24 && dstFormat != AV_PIX_FMT_YUV420P)) { - enum AVPixelFormat tmpFormat = AV_PIX_FMT_RGB24; + (dstFormat != AV_PIX_FMT_RGB24 && dstFormat != AV_PIX_FMT_YUV420P && + dstFormat != AV_PIX_FMT_RGB48)) { + enum AVPixelFormat tmpFormat = isBayer16BPS(srcFormat) ? AV_PIX_FMT_RGB48 : AV_PIX_FMT_RGB24; ret = av_image_alloc(c->cascaded_tmp, c->cascaded_tmpStride, srcW, srcH, tmpFormat, 64); |