用FFmpeg在C++中解码HEVC文件缺少一帧



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

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

有人能帮我吗?我把代码贴在这里。抱歉,不知道如何附加我的项目和测试文件。非常感谢!

谨致问候,Ivan

#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, "P6n%d %dn%dn", 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 decodingn");
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 decodingn");
exit(1);
}
printf("saving frame %3dn", 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 foundn");
exit(1);
}
parser = av_parser_init(codec->id);
if (!parser) {
fprintf(stderr, "parser not foundn");
exit(1);
}
codecContext = avcodec_alloc_context3(codec);
if (!codecContext) {
fprintf(stderr, "Could not allocate video codec contextn");
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 codecn");
exit(1);
}
fopen_s(&file, filename, "rb");
if (!file) {
fprintf(stderr, "Could not open %sn", filename);
exit(1);
}
frame = av_frame_alloc();
if (!frame) {
fprintf(stderr, "Could not allocate video framen");
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 parsingn");
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文档:

buf_size:输入长度,以信号EOF,这应该是0(以便可以输出最后一帧(。

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

[编辑]

为了清楚起见,我承认您正确地复制了API文档中的示例代码:

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

然而,不幸的是,该代码不完整。如果您查看demux.c中该代码的相关用法,就会发现需要显式刷新:

[..]
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);
[..]

最新更新