From 4eb55942953bd4edba674625a0100aea348bb4c4 Mon Sep 17 00:00:00 2001 From: Ramiro Polla Date: Fri, 9 Aug 2024 13:26:09 +0200 Subject: [PATCH] swscale: add nv24/nv42 to yuv420p unscaled converter --- libswscale/swscale_unscaled.c | 45 +++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/libswscale/swscale_unscaled.c b/libswscale/swscale_unscaled.c index a5c9917799..239258ab8c 100644 --- a/libswscale/swscale_unscaled.c +++ b/libswscale/swscale_unscaled.c @@ -221,6 +221,48 @@ static int nv24ToPlanarWrapper(SwsContext *c, const uint8_t *src[], return srcSliceH; } +static void nv24_to_yuv420p_chroma(uint8_t *dst1, int dstStride1, + uint8_t *dst2, int dstStride2, + const uint8_t *src, int srcStride, + int w, int h) +{ + const uint8_t *src1 = src; + const uint8_t *src2 = src + srcStride; + // average 4 pixels into 1 (interleaved U and V) + for (int y = 0; y < h; y += 2) { + for (int x = 0; x < w; x++) { + dst1[x] = (src1[4 * x + 0] + src1[4 * x + 2] + + src2[4 * x + 0] + src2[4 * x + 2]) >> 2; + dst2[x] = (src1[4 * x + 1] + src1[4 * x + 3] + + src2[4 * x + 1] + src2[4 * x + 3]) >> 2; + } + src1 += srcStride * 2; + src2 += srcStride * 2; + dst1 += dstStride1; + dst2 += dstStride2; + } +} + +static int nv24ToYuv420Wrapper(SwsContext *c, const uint8_t *src[], + int srcStride[], int srcSliceY, int srcSliceH, + uint8_t *dstParam[], int dstStride[]) +{ + uint8_t *dst1 = dstParam[1] + dstStride[1] * srcSliceY / 2; + uint8_t *dst2 = dstParam[2] + dstStride[2] * srcSliceY / 2; + + copyPlane(src[0], srcStride[0], srcSliceY, srcSliceH, c->srcW, + dstParam[0], dstStride[0]); + + if (c->srcFormat == AV_PIX_FMT_NV24) + nv24_to_yuv420p_chroma(dst1, dstStride[1], dst2, dstStride[2], + src[1], srcStride[1], c->srcW / 2, srcSliceH); + else + nv24_to_yuv420p_chroma(dst2, dstStride[2], dst1, dstStride[1], + src[1], srcStride[1], c->srcW / 2, srcSliceH); + + return srcSliceH; +} + static int planarToP01xWrapper(SwsContext *c, const uint8_t *src8[], int srcStride[], int srcSliceY, int srcSliceH, uint8_t *dstParam8[], @@ -2206,6 +2248,9 @@ void ff_get_unscaled_swscale(SwsContext *c) c->convert_unscaled = yuyvToYuv422Wrapper; if (srcFormat == AV_PIX_FMT_UYVY422 && dstFormat == AV_PIX_FMT_YUV422P) c->convert_unscaled = uyvyToYuv422Wrapper; + if (dstFormat == AV_PIX_FMT_YUV420P && + (srcFormat == AV_PIX_FMT_NV24 || srcFormat == AV_PIX_FMT_NV42)) + c->convert_unscaled = nv24ToYuv420Wrapper; #define isPlanarGray(x) (isGray(x) && (x) != AV_PIX_FMT_YA8 && (x) != AV_PIX_FMT_YA16LE && (x) != AV_PIX_FMT_YA16BE) /* simple copy */