FFMPEG and STB_Image Create awful Picture

130 Views Asked by At

I was learning how to use the FFMPEG C api and I was trying to encode a jpeg into a MPEG file. I load the JPEG into (unsigned char *) using the stb-image library. Then I create a (uint8_t *) and copy my rgb values. Finally, I convert RGB to YUV420 using sws_scale. However, a portion of my image blurs out when I perform the encoding.Bad Image / This is the original imageoriginal image
Perhaps I allocate my frame buffer incorrectly?

ret = av_frame_get_buffer(frame, 0);


This is my entire program

#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;
}

0

There are 0 best solutions below