You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用FFmpeg C API与STB_Image编码JPEG为MPEG时图像模糊问题

问题:FFmpeg编码JPEG为MPEG后图像部分模糊

我正在学习FFmpeg C API,尝试将一张JPEG图片编码为MPEG文件。通过STB_Image库加载JPEG到unsigned char*,复制为uint8_t*后用sws_scale将RGB转YUV420格式,但编码后图像部分区域模糊。

错误效果图:
模糊图像

原始图像:
原始图像

我怀疑是帧缓冲区分配的问题,相关代码:

ret = av_frame_get_buffer(frame, 0);

完整程序代码:

#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"
#define STB_IMAGE_RESIZE_IMPLEMENTATION
#include "stb_image_resize.h"
#include <assert.h>

#include <libavcodec/avcodec.h>
#include <libavutil/opt.h>
#include <libavutil/imgutils.h>
#include <libswscale/swscale.h>
//gcc stack.c -lm -o stack.o `pkg-config --cflags --libs libavformat libavcodec libswresample libswscale libavutil` && ./stack.o

/*
int i : pts of current frame
*/
void PictureToFrame(int i, AVFrame *frame, int height, int width)
{
    //Use stb image to get rgb values
        char *fileName = "profil.jpeg";
        int imageHeight = 0;
        int imageWidth = 0;
        int colorChannels = 0;
        int arrayLength = 0;
    unsigned char *image = stbi_load(fileName,&imageWidth,&imageHeight,&colorChannels,0);
    
    printf("(height: %d, width: %d)\n",imageHeight, imageWidth);
    assert(colorChannels == 3 && imageHeight == height && imageWidth == width);
    
    //Convert unsigned char * to uint8_t *
    arrayLength = imageHeight * imageWidth * colorChannels;
    uint8_t *rgb = calloc(arrayLength, sizeof(uint8_t));
    int j = arrayLength-1;
    for(int i = 0; i < arrayLength; i++)
    {
        rgb[i] = (uint8_t) image[i];
        }
        
        //Use SwsContext to scale RGB to YUV420P and write to frame
        const int in_linesize[1] = { 3* imageWidth};
        struct SwsContext *sws_context = NULL;
        sws_context = sws_getCachedContext(sws_context,
            imageWidth, imageHeight, AV_PIX_FMT_RGB24,
            imageWidth, imageHeight, AV_PIX_FMT_YUV420P,
            0, 0, 0, 0);
        sws_scale(sws_context, (const uint8_t * const *)&rgb, in_linesize, 0,
            imageHeight, frame->data, frame->linesize);
        //Save frame pts
        frame->pts = i;
        
        //Free alloc'd data
        stbi_image_free(image);
        sws_freeContext(sws_context);
        free(rgb);
}
static void encode(AVCodecContext *enc_ctx, AVFrame *frame, AVPacket *pkt, FILE *outfile)
{
    int returnValue;
    /* send the frame to the encoder */
    if(frame)
    {
        printf("Send frame %3"PRId64"\n", frame->pts);
    }
        returnValue = avcodec_send_frame(enc_ctx, frame);
    if(returnValue < 0)
    {
        printf("Error sending a frame for encoding\n");
        return;
    }
    while(returnValue >= 0)
    {
        returnValue = avcodec_receive_packet(enc_ctx, pkt);
        if(returnValue == AVERROR(EAGAIN) || returnValue == AVERROR_EOF)
        {
            return;
        }
        else if(returnValue < 0)
        {
            printf("Error during encoding\n");
            return;
        }

        printf("Write packet %3"PRId64" (size=%5d)\n", pkt->pts, pkt->size);
        fwrite(pkt->data, 1, pkt->size, outfile);
        av_packet_unref(pkt);
    }
}


int main(int argc, char **argv)
{
    const char *filename, *codec_name;
    const AVCodec *codec;
    AVCodecContext *c= NULL;
    int i, ret, x, y;
    FILE *f;
    AVFrame *frame;
    AVPacket *pkt;
    uint8_t endcode[] = { 0, 0, 1, 0xb7 };

    filename = "outo.mp4";
    codec_name = "mpeg1video";//"mpeg1video";//"libx264";


    /* find the mpeg1video encoder */
    codec = avcodec_find_encoder_by_name(codec_name);
    if(!codec)
    {
        printf("Error finding codec\n");
    return 0;
    }

    c = avcodec_alloc_context3(codec);
    if(!c)
    {
        printf("Error allocating c\n");
    return 0;
    }

    pkt = av_packet_alloc();
    if(!pkt)
    {
        printf("Error allocating pkt\n");
    return 0;
    }

    /* put sample parameters */
    c->bit_rate = 400000;
    /* resolution must be a multiple of two */
    c->width = 800;
    c->height = 800;
    /* frames per second */
    c->time_base = (AVRational){1, 25};
    c->framerate = (AVRational){25, 1};
    c->gop_size = 10;
    c->max_b_frames = 1;
    c->pix_fmt = AV_PIX_FMT_YUV420P;

    if(codec->id == AV_CODEC_ID_H264)
    {
        av_opt_set(c->priv_data, "preset", "slow", 0);
    }
        

    /* open it */
    ret = avcodec_open2(c, codec, NULL);
    if(ret < 0) 
    {
        printf("Error opening codec\n");
    return 0;
    }

    f = fopen(filename, "wb");
    if(!f)
    {
         printf("Error opening file\n");
     return 0;
    }

    frame = av_frame_alloc();
    if(!frame)
    {
        printf("Error allocating frame\n");
    return 0;
    }
    frame->format = c->pix_fmt;
    frame->width  = c->width;
    frame->height = c->height;

    //I suspect this is the problem
    ret = av_frame_get_buffer(frame, 0);
    if(ret < 0)
    {
        fprintf(stderr, "Could not allocate the video frame data\n");
        exit(1);
    }

    /* encode 25 frames*/
    for(i = 0; i < 25; i++) 
    {

        /* make sure the frame data is writable */
        ret = av_frame_make_writable(frame);
        if(ret < 0)
        {
            return 0;
        }
        //FIll Frame with picture data
        PictureToFrame(i, frame, c->height, c->width);

        /* encode the image */
        encode(c, frame, pkt, f);
    }

    /* flush the encoder */
    encode(c, NULL, pkt, f);

    /* add sequence end code to have a real MPEG file */
    if (codec->id == AV_CODEC_ID_MPEG1VIDEO || codec->id == AV_CODEC_ID_MPEG2VIDEO)
        fwrite(endcode, 1, sizeof(endcode), f);
    fclose(f);

    avcodec_free_context(&c);
    av_frame_free(&frame);
    av_packet_free(&pkt);

    return 0;
}

问题分析与修复

你的av_frame_get_buffer调用没有问题,图像模糊是由以下几个代码错误导致:

1. 变量名冲突导致PTS异常

PictureToFrame函数的参数int i用于设置帧的PTS,但你在循环里又定义了同名的int i,覆盖了参数值。循环结束后i等于图像数据总长度,导致所有帧的PTS被设置成同一个超大值,编码器时序混乱,引发图像异常。

修复:修改循环变量名,避免冲突:

for(int k = 0; k < arrayLength; k++)
{
    rgb[k] = (uint8_t) image[k];
}

2. 图像格式字节顺序不匹配

STB加载的JPEG是RGB字节顺序,但FFmpeg的AV_PIX_FMT_RGB24实际是BGR字节顺序(历史遗留定义)。直接用AV_PIX_FMT_RGB24作为输入格式,颜色空间转换时会出现通道错乱,表现为图像模糊或颜色失真。

修复二选一:

  • 方案1:修改SwsContext的输入格式为AV_PIX_FMT_BGR24
sws_context = sws_getCachedContext(sws_context,
    imageWidth, imageHeight, AV_PIX_FMT_BGR24,  // 替换为BGR24
    imageWidth, imageHeight, AV_PIX_FMT_YUV420P,
    SWS_BILINEAR, 0, 0, 0);
  • 方案2:将STB加载的RGB手动转为BGR
unsigned char *image = stbi_load(fileName,&imageWidth,&imageHeight,&colorChannels,STBI_rgb);
// 遍历交换R和B通道
for(int k = 0; k < arrayLength; k +=3) {
    uint8_t temp = image[k];
    image[k] = image[k+2];
    image[k+2] = temp;
}

3. 不必要的内存复制

unsigned char和uint8_t是等价类型(都是1字节无符号整数),不需要用calloc重新分配内存并复制,直接使用stbi_load返回的image指针即可,减少内存开销:

// 删除这段冗余的复制代码
/*
arrayLength = imageHeight * imageWidth * colorChannels;
uint8_t *rgb = calloc(arrayLength, sizeof(uint8_t));
int j = arrayLength-1;
for(int k = 0; k < arrayLength; k++)
{
    rgb[k] = (uint8_t) image[k];
}
*/
// 直接传入image给sws_scale
sws_scale(sws_context, (const uint8_t * const *)&image, in_linesize, 0,
    imageHeight, frame->data, frame->linesize);
// 最后只释放image,无需释放rgb

4. 输出容器与编码格式不匹配

你用mpeg1video编码器生成的是MPEG-1裸流,但输出文件名是outo.mp4。MP4是容器格式,直接写入裸流会导致播放器解析异常,建议将输出文件名改为outo.mpg或outo.mpeg。


内容的提问来源于stack exchange,提问作者murage kibicho

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 05:36:00