swscaler @ 0x8afba0] deprecated pixel format used, make sure you did set range correctly


int save_frame_as_jpeg(AVFrame *pFrame, int width, int height, int iFrame) {
    int ret = 0;

    const AVCodec *jpegCodec = avcodec_find_encoder(AV_CODEC_ID_MJPEG);
    if (!jpegCodec) {
        fprintf(stderr, "JPEG encoder not found\n");
        return -1;
    }

    AVCodecContext *jpegContext = avcodec_alloc_context3(jpegCodec);
    if (!jpegContext) {
        fprintf(stderr, "Could not allocate JPEG codec context\n");
        return -1;
    }

    jpegContext->pix_fmt = AV_PIX_FMT_YUVJ420P; // Поддерживаемый формат
		
    jpegContext->width = width;
    jpegContext->height = height;
    jpegContext->time_base = (AVRational){1, 25};

    if ((ret = avcodec_open2(jpegContext, jpegCodec, NULL)) < 0) {
        fprintf(stderr, "Could not open JPEG codec\n");
        avcodec_free_context(&jpegContext);
        return -1;
    }

    // Конвертация RGB24 -> YUVJ420P
    struct SwsContext *sws_ctx = sws_getContext(width, height, pFrame->format,
                                                width, height, jpegContext->pix_fmt,
                                                SWS_BILINEAR, NULL, NULL, NULL);
    if (!sws_ctx) {
        fprintf(stderr, "Could not create sws context\n");
        avcodec_free_context(&jpegContext);
        return -1;
    }

    AVFrame *frame_yuv = av_frame_alloc();
    if (!frame_yuv) {
        fprintf(stderr, "Could not allocate frame for YUV\n");
        sws_freeContext(sws_ctx);
        avcodec_free_context(&jpegContext);
        return -1;
    }

    frame_yuv->format = jpegContext->pix_fmt;
    frame_yuv->width = width;
    frame_yuv->height = height;

    if ((ret = av_frame_get_buffer(frame_yuv, 32)) < 0) {
        fprintf(stderr, "Could not allocate buffer for YUV frame\n");
        av_frame_free(&frame_yuv);
        sws_freeContext(sws_ctx);
        avcodec_free_context(&jpegContext);
        return -1;
    }

    sws_scale(sws_ctx, (const uint8_t * const *)pFrame->data, pFrame->linesize, 0, height,
              frame_yuv->data, frame_yuv->linesize);
    sws_freeContext(sws_ctx);

    AVPacket *pkt = av_packet_alloc();
    if (!pkt) {
        fprintf(stderr, "Could not allocate packet\n");
        av_frame_free(&frame_yuv);
        avcodec_free_context(&jpegContext);
        return -1;
    }

    ret = avcodec_send_frame(jpegContext, frame_yuv);
    av_frame_free(&frame_yuv);

    if (ret < 0) {
        fprintf(stderr, "Error sending frame to encoder\n");
        av_packet_free(&pkt);
        avcodec_free_context(&jpegContext);
        return -1;
    }

    ret = avcodec_receive_packet(jpegContext, pkt);
    if (ret < 0) {
        fprintf(stderr, "Error receiving packet from encoder\n");
        av_packet_free(&pkt);
        avcodec_free_context(&jpegContext);
        return -1;
    }

 
		char filename[128];
		snprintf(filename, sizeof(filename), "/tmp/frame_%d.jpg", iFrame);

    FILE *JPEGFile = fopen(filename, "wb");
    if (!JPEGFile) {
        perror("Failed to open output file");
        av_packet_unref(pkt);
        av_packet_free(&pkt);
        avcodec_free_context(&jpegContext);
        return -1;
    }

    fwrite(pkt->data, 1, pkt->size, JPEGFile);
    fclose(JPEGFile);
    //printf("Saved frame: %s (%d KB)\n", filename, pkt->size / 1024);

    av_packet_unref(pkt);
    av_packet_free(&pkt);
    avcodec_free_context(&jpegContext);

    return 0;
}