scale – FFMPEG convert NV12 format to NV12 with the same height and width

I want to use FFmpeg to convert the input NV12 format to output NV12 format with the same height and width. I used sws_scale conversion, but the output frame’s colors are all green.

P.S. It seems no need to use swscale to get the same width,same height and same format frame,but it is neccessary in my project for dealing with other frames.

I have successfully converted the input NV12 format to output NV12 format with the different height and width, the output frame’s colors were right.But I FAILED to convert NV12 to NV12 with the same height and width. It was so weird, I couldn’t know why:(

I want to know what the reason is and what I should do.
The following is my code.
Thank you for you help~

    AVFrame* frame_nv12 = av_frame_alloc();
    frame_nv12->width = in_width;
    frame_nv12->height = in_height;
    frame_nv12->format = AV_PIX_FMT_NV12;
    uint8_t* frame_buffer_nv12 = (uint8_t*)av_malloc(av_image_get_buffer_size(AV_PIX_FMT_NV12, in_width, in_height , 1));
    av_image_fill_arrays(frame_nv12->data, frame_nv12->linesize, frame_buffer_nv12, AV_PIX_FMT_NV12, in_width, in_height, 1);

    AVFrame* frame2_nv12 = av_frame_alloc();
    frame2_nv12->width = in_width;
    frame2_nv12->height = in_height;
    frame2_nv12->format = AV_PIX_FMT_NV12;
    uint8_t* frame2_buffer_nv12 = (uint8_t*)av_malloc(av_image_get_buffer_size(AV_PIX_FMT_NV12, in_width, in_height, 1));
    av_image_fill_arrays(frame2_nv12->data, frame2_nv12->linesize, frame2_buffer_nv12, AV_PIX_FMT_NV12, in_width, in_height, 1);

     SwsContext* swsCtx4 = nullptr;
    swsCtx4 = sws_getContext(in_width, in_height, AV_PIX_FMT_NV12, in_width, in_height, AV_PIX_FMT_NV12,SWS_BILINEAR | SWS_PRINT_INFO, NULL, NULL, NULL);

        int ret = sws_scale(swsCtx4, frame_nv12->data, frame_nv12->linesize, 0, frame_nv12->height, frame2_nv12->data, frame2_nv12->linesize);
        if (ret < 0) {
            printf("sws_1scale failed\n");

Read more here: Source link