在 C++ 中解码 HEVC 文件,FFmpeg 丢失一帧

Decoding HEVC file in C++ with FFmpeg missing one frame

我正在尝试使用 FFmpeg 在 C++ 中解码我的 Hevc 文件。我使用了 Hevc 解码器并尝试以 ppm 格式保存帧(几乎整个源代码来自 FFmpeg 示例 [decode_video.c] https://ffmpeg.org/doxygen/trunk/decode_video_8c-example.html,新的是从 yuv 到 rgb 的转换)。我的 Hevc 文件有 677 帧,我在命令 window 中使用 ffprobe 检查了这一点。但是我的项目有 676 帧。我还检查了其他 Hevc 文件,结果是一样的,我总是少一帧。 我也尝试了另一个FFmpeg示例[demuxing_decoding.c](https://ffmpeg.org/doxygen/trunk/demuxing_decoding_8c-example.html),结果是一样的,少了一帧...

似乎对 H265 和 H264 文件很满意,这是 FFmpeg 的错误吗?

任何人都可以帮助我,我 post 我的代码在这里。抱歉,不知道如何附加我的项目和测试文件。非常感谢!

此致, 伊万

#include <iostream>

extern "C"
{
#include "../Headers/libavcodec/avcodec.h"
#include "../Headers/libavformat/avformat.h"
#include "../Headers/libswscale/swscale.h"
}

#define INBUF_SIZE 4096

//Save RGB image as PPM file format
static void ppm_save(char* filename, AVFrame* frame)
{
    FILE* file;
    int i;

    fopen_s(&file, filename, "wb");
    fprintf(file, "P6\n%d %d\n%d\n", frame->width, frame->height, 255);
    for (i = 0; i < frame->height; i++)
        fwrite(frame->data[0] + i * frame->linesize[0], 1, frame->width * 3, file);
    fclose(file);
}

void decode(AVCodecContext* dec_ctx, AVFrame* frame, AVPacket* pkt, const char* outfilePrefix)
{
    char buf[1024];
    int ret;

    ret = avcodec_send_packet(dec_ctx, pkt);
    if (ret < 0) {
        fprintf(stderr, "Error sending a packet for decoding\n");
        exit(1);
    }

    int sts;
    ////////////////////////////////////////////////////////////////////////////
    //Create SWS Context for converting from decode pixel format (like YUV420) to RGB
    struct SwsContext* sws_ctx = NULL;
    sws_ctx = sws_getContext(dec_ctx->width,
        dec_ctx->height,
        dec_ctx->pix_fmt,
        dec_ctx->width,
        dec_ctx->height,
        AV_PIX_FMT_RGB24,
        SWS_BICUBIC,
        NULL,
        NULL,
        NULL);

    if (sws_ctx == nullptr)
    {
        return;  //Error!
    }

    //Allocate frame for storing image converted to RGB.
    AVFrame* pRGBFrame = av_frame_alloc();

    pRGBFrame->format = AV_PIX_FMT_RGB24;
    pRGBFrame->width = dec_ctx->width;
    pRGBFrame->height = dec_ctx->height;
    sts = av_frame_get_buffer(pRGBFrame, 0);

    if (sts < 0)
    {
        goto free;
        //return;  //Error!
    }

    while (ret >= 0)
    {
        ret = avcodec_receive_frame(dec_ctx, frame);
        if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
            goto free;
        //return;
        else if (ret < 0) {
            fprintf(stderr, "Error during decoding\n");
            exit(1);
        }

        printf("saving frame %3d\n", dec_ctx->frame_number);//
        fflush(stdout);

        //////////////////////////////////////////////////////////////////////////
        //Convert from input format (e.g YUV420) to RGB and save to PPM:
        sts = sws_scale(sws_ctx,    //struct SwsContext* c,
            frame->data,            //const uint8_t* const srcSlice[],
            frame->linesize,        //const int srcStride[],
            0,                      //int srcSliceY, 
            frame->height,          //int srcSliceH,
            pRGBFrame->data,        //uint8_t* const dst[], 
            pRGBFrame->linesize);   //const int dstStride[]);

        snprintf(buf, sizeof(buf), "%s-%d.ppm", outfilePrefix, dec_ctx->frame_number);
        ppm_save(buf, pRGBFrame);
    }

free:
    //Free
    ////////////////////////////////////////////////////////////////////////////
    sws_freeContext(sws_ctx);
    av_frame_free(&pRGBFrame);
}

int main()
{
    const char* filename, * outfilePrefix, * seqfilename;
    const AVCodec* codec;
    AVCodecParserContext* parser;
    AVCodecContext* codecContext = NULL;
    FILE* file;
    AVFrame* frame;
    uint8_t inbuf[INBUF_SIZE + AV_INPUT_BUFFER_PADDING_SIZE];
    uint8_t* data;
    size_t   data_size;
    int ret;
    AVPacket* pkt;

#ifdef _DEBUG
    filename = "D:\TestFiles\sample_1280x720.hevc";
    outfilePrefix = "D:\TestFiles\sample_1280x720_output\output";
#else
    if (argc <= 2) {
        fprintf(stderr, "Usage: %s <input file> <output file>\n"
            "And check your input file is encoded by mpeg1video please.\n", argv[0]);
        exit(0);
    }
    filename = argv[1];
    outfilePrefix = argv[2];
#endif

    pkt = av_packet_alloc();
    if (!pkt)
        exit(1);

    /* set end of buffer to 0 (this ensures that no overreading happens for damaged MPEG streams) */
    memset(inbuf + INBUF_SIZE, 0, AV_INPUT_BUFFER_PADDING_SIZE);

    /* find the HEVC video decoder */
    codec = avcodec_find_decoder(AV_CODEC_ID_HEVC);
    if (!codec) {
        fprintf(stderr, "Codec not found\n");
        exit(1);
    }

    parser = av_parser_init(codec->id);
    if (!parser) {
        fprintf(stderr, "parser not found\n");
        exit(1);
    }

    codecContext = avcodec_alloc_context3(codec);
    if (!codecContext) {
        fprintf(stderr, "Could not allocate video codec context\n");
        exit(1);
    }

    /* For some codecs, such as msmpeg4 and mpeg4, width and height
       MUST be initialized there because this information is not
       available in the bitstream. */

       /* open it */
    if (avcodec_open2(codecContext, codec, NULL) < 0) {
        fprintf(stderr, "Could not open codec\n");
        exit(1);
    }

    fopen_s(&file, filename, "rb");
    if (!file) {
        fprintf(stderr, "Could not open %s\n", filename);
        exit(1);
    }

    frame = av_frame_alloc();
    if (!frame) {
        fprintf(stderr, "Could not allocate video frame\n");
        exit(1);
    }

    while (!feof(file)) {
        /* read raw data from the input file */
        data_size = fread(inbuf, 1, INBUF_SIZE, file);
        if (!data_size)
            break;

        /* use the parser to split the data into frames */
        data = inbuf;
        while (data_size > 0)
        {
            ret = av_parser_parse2(parser, codecContext, &pkt->data, &pkt->size,
                data, data_size, AV_NOPTS_VALUE, AV_NOPTS_VALUE, 0);
            if (ret < 0) {
                fprintf(stderr, "Error while parsing\n");
                exit(1);
            }
            data += ret;
            data_size -= ret;

            if (pkt->size)
                decode(codecContext, frame, pkt, outfilePrefix);
        }
    }

    /* flush the decoder */
    decode(codecContext, frame, NULL, outfilePrefix);

    fclose(file);

    av_parser_close(parser);
    avcodec_free_context(&codecContext);
    av_frame_free(&frame);
    av_packet_free(&pkt);
}

问题是您没有用 data_size=0 调用 av_parser_parse2() 来发出 EOF 信号。见 API docs:

buf_size: input length, to signal EOF, this should be 0 (so that the last frame can be output).

如果没有该调用,一帧将缓存在解析器中,这就是您的输出中缺少的那一帧。

[编辑]

明确地说,我确认您正确复制了 API 文档中的示例代码:

[..]
while(in_len){
    len = av_parser_parse2(myparser, AVCodecContext, &data, &size,
                                     in_data, in_len,
                                     pts, dts, pos);
[..]

但是,遗憾的是该代码不完整。如果您查看 demux.c 中该代码的 relevant usage,您会发现需要显式刷新:

[..]
1134     while (size > 0 || (flush && got_output)) {
1135         int64_t next_pts = pkt->pts;
1136         int64_t next_dts = pkt->dts;
1137         int len;
1138 
1139         len = av_parser_parse2(sti->parser, sti->avctx,
1140                                &out_pkt->data, &out_pkt->size, data, size,
1141                                pkt->pts, pkt->dts, pkt->pos);
[..]