1
0
mirror of https://github.com/FFmpeg/FFmpeg.git synced 2024-12-23 12:43:46 +02:00

swscale: do not drop half of bits from 16bit bayer formats

This commit is contained in:
Paul B Mahol 2020-08-02 15:55:38 +02:00
parent b9ff25a93e
commit 9d58cdb4ba
4 changed files with 163 additions and 2 deletions

View File

@ -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

View File

@ -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);

View File

@ -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)) {

View File

@ -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);