'After decode a package, frame data looks like error

I am using FFmpeg version 4.4_2 on macOS 11.3.1

I use avcodec_send_packet() and avcodec_receive_frame() to decode a package. both of them return 0. But the frame data after decode looks like this:

frame->data[0] = "\xeb\xeb\xeb\xeb\xeb\xeb...."
frame->data[1] = "\x80\x80\x80\x80\x80\x80...."
frame->data[2] = "\x80\x80\x80\x80\x80\x80...."

decode every package will get the same data,I write a demo code like this:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

extern "C"
{
#include "libavformat/avformat.h"
#include "libswscale/swscale.h"
#include "libavutil/imgutils.h"
#include "libswresample/swresample.h"
}
#define INBUF_SIZE 4096
int main(int argc, const char * argv[])
{
    const char *filename    = "2.mp4";
    const char *outfilename = "1.yuv";
    AVFormatContext    *pFormatCtx;
    int                i, videoindex;
    AVCodecContext    *pCodecCtx;
    AVCodec            *pCodec;
    AVFrame    *pFrame,*pFrameYUV;
    uint8_t *out_buffer;
    AVPacket *packet;
    int y_size;
    int ret, got_picture;
    struct SwsContext *img_convert_ctx;
    FILE *fp_yuv;
    int frame_cnt;
    clock_t time_start, time_finish;
    double  time_duration = 0.0;
    
    av_register_all();
    pFormatCtx = avformat_alloc_context();
    if(avformat_open_input(&pFormatCtx,filename,NULL,NULL)!=0)
    {
        printf("Couldn't open input stream.\n");
        return 0;
    }
    if(avformat_find_stream_info(pFormatCtx,NULL)<0)
    {
        printf("Couldn't find stream information.\n");
        return 0;
    }
    videoindex=-1;
    for(i=0; i<pFormatCtx->nb_streams; i++)
    {
        if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO)
        {
            videoindex=i;
            break;
        }
    }
    if(videoindex==-1)
    {
        printf("Couldn't find a video stream.\n");
        return 0;
    }
    pCodecCtx=pFormatCtx->streams[videoindex]->codec;
    pCodec=avcodec_find_decoder(pCodecCtx->codec_id);
    if(pCodec==NULL){
        printf("Couldn't find Codec.\n");
        return 0;
    }
    if(avcodec_open2(pCodecCtx, pCodec,NULL)<0){
        printf("Couldn't open codec.\n");
        return 0;
    }
    pFrame=av_frame_alloc();
    pFrameYUV=av_frame_alloc();
    out_buffer=(unsigned char *)av_malloc(av_image_get_buffer_size(AV_PIX_FMT_YUV420P,  pCodecCtx->width, pCodecCtx->height,1));
    av_image_fill_arrays(pFrameYUV->data, pFrameYUV->linesize,out_buffer,
                             AV_PIX_FMT_YUV420P,pCodecCtx->width, pCodecCtx->height,1);
    
    packet=(AVPacket *)av_malloc(sizeof(AVPacket));
    img_convert_ctx = sws_getContext(pCodecCtx->width, pCodecCtx->height, AV_PIX_FMT_RGB24,
                                         pCodecCtx->width, pCodecCtx->height, AV_PIX_FMT_YUV420P, SWS_BICUBIC, NULL, NULL, NULL);
    fp_yuv=fopen(outfilename,"wb+");
    if(fp_yuv==NULL)
    {
        printf("Cannot open output file.\n");
        return 0;
    }
    frame_cnt=0;
    time_start = clock();
    while(av_read_frame(pFormatCtx, packet)>=0)
    {
        if(packet->stream_index==videoindex)
        {
            int decodeValue = avcodec_send_packet(pCodecCtx, packet);
            if (decodeValue < 0)
            {
                continue;
            }
            int got_picture = avcodec_receive_frame(pCodecCtx, pFrame);
            if (got_picture < 0)
            {
                continue;
            }
            sws_scale(img_convert_ctx, (const uint8_t* const*)pFrame->data, pFrame->linesize, 0, pCodecCtx->height,
                          pFrameYUV->data, pFrameYUV->linesize);
            y_size=pCodecCtx->width*pCodecCtx->height;
            fwrite(pFrameYUV->data[0],1,y_size,fp_yuv);    //Y
            fwrite(pFrameYUV->data[1],1,y_size/4,fp_yuv);  //U
            fwrite(pFrameYUV->data[2],1,y_size/4,fp_yuv);  //V
            //Output info
            char pictype_str[10]={0};
            switch(pFrame->pict_type)
            {
                case AV_PICTURE_TYPE_I:sprintf(pictype_str,"I");break;
                case AV_PICTURE_TYPE_P:sprintf(pictype_str,"P");break;
                case AV_PICTURE_TYPE_B:sprintf(pictype_str,"B");break;
                default:sprintf(pictype_str,"Other");break;
            }
            printf("Frame Index: %5d. Type:%s\n",frame_cnt,pictype_str);
            frame_cnt++;
        }
        av_free_packet(packet);
    }
    time_finish = clock();
    time_duration=(double)(time_finish - time_start);
    sws_freeContext(img_convert_ctx);
    fclose(fp_yuv);
    av_frame_free(&pFrameYUV);
    av_frame_free(&pFrame);
    avcodec_close(pCodecCtx);
    avformat_close_input(&pFormatCtx);
}

Has anyone encountered the same problem? Please tell me the solution.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source