[Libav-user] How to change color range on SwsContext

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

[Libav-user] How to change color range on SwsContext

GRANGER Nicolas
Hi everyone,

I need to perform lossless encoding of grayscale videos, which involves
a color space conversion step: GRAY8->YUV420. Lossless conversion means
the full color range must be preserved which is not the case by default
with AV_PIX_FMT_YUV420P.

When creating an instance of SwsContext with parameters
AV_PIX_FMT_GRAY8 and AV_PIX_FMT_YUVJ420P, it works but issues a warning
that AV_PIX_FMT_YUVJ420P is deprecated.

I have tried to create a context with AV_PIX_FMT_YUV420P as the
destination color space and then override the color range but it fails
with operation not permitted (see example below). It can be done with
the command line tools (arguments: "-pix_fmt yuv420p -dst_range 1
-color_range 2") but how to reproduce it with the C API?


-------

#include <libavcodec/avcodec.h>
#include <libswscale/swscale.h>


int main() {
    struct SwsContext* ctx = sws_getContext(
        800, 600, AV_PIX_FMT_GRAY8,
        800, 600, AV_PIX_FMT_YUV420P, // works with AV_PIX_FMT_YUVJ420P
        SWS_POINT, NULL, NULL, NULL);

    // fix color ranges
    int dummy[4];
    int srcRange, dstRange;
    int brightness, contrast, saturation;
    sws_getColorspaceDetails(ctx, (int **)&dummy, &srcRange,
                           (int **)&dummy, &dstRange, &brightness,
&contrast,
                           &saturation);

    const int *coefs = sws_getCoefficients(SWS_CS_DEFAULT);

    int err = sws_setColorspaceDetails(
      ctx, coefs, srcRange, coefs, 1,
      brightness, contrast, saturation);

    if (err < 0)
        printf(av_err2str(err));

    sws_freeContext(ctx);

    return 0;
}

-------
_______________________________________________
Libav-user mailing list
[hidden email]
https://ffmpeg.org/mailman/listinfo/libav-user

To unsubscribe, visit link above, or email
[hidden email] with subject "unsubscribe".