使用libavcodec库解码H264视频,C语言

3

我正在尝试使用ffmpeg/libavcodec解码原始h264文件,但无法使其正常工作。目前的输出应该是一个原始的YUV文件。可以使用GCC编译代码。

gcc -o decoder decoder.c -L./lib/ -llibavcodec -llibavutil

avcodec.dll、avutil.dll和swresample.dll必须放置在.exe文件所在的目录中才能启动。CMD中的输出如下(只是部分内容,但始终如此):

[h264 @ 00a80f20] reference picture missing during reorder
[h264 @ 00a80f20] Missing reference picture, default is 65562
[h264 @ 00a80f20] error while decoding MB 80 54, bytestream -10
[h264 @ 00a80f20] concealing 1649 DC, 1649 AC, 1649 MV errors in B frame
[h264 @ 00a80f20] reference picture missing during reorder
[h264 @ 00a80f20] reference picture missing during reorder
[h264 @ 00a80f20] reference picture missing during reorder
[h264 @ 00a80f20] Missing reference picture, default is 65566
[h264 @ 00a80f20] Missing reference picture, default is 65566
[h264 @ 00a80f20] Missing reference picture, default is 65566
[h264 @ 00a80f20] reference picture missing during reorder
[h264 @ 00a80f20] Missing reference picture, default is 65568
[h264 @ 00a80f20] reference picture missing during reorder
[h264 @ 00a80f20] Missing reference picture, default is 65570
[h264 @ 00a80f20] reference picture missing during reorder

这是我的代码。
#include <stdlib.h>
#include <stdio.h>

#ifdef HAVE_AV_CONFIG_H
#undef HAVE_AV_CONFIG_H
#endif

#include "libavcodec/avcodec.h"
//#include "libavcodec/libavutil/mathematics.h"

#define INBUF_SIZE 4096

void video_decode(char *outfilename, char *filename)
{
    AVCodec *codec;
    AVCodecContext *c= NULL;
    int frame, got_picture, len;
    FILE *f, *outf;
    AVFrame *picture;
    uint8_t inbuf[INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE];
    AVPacket avpkt;
    int i;

    av_init_packet(&avpkt);

    memset(inbuf + INBUF_SIZE, 0, FF_INPUT_BUFFER_PADDING_SIZE);

    codec = avcodec_find_decoder(AV_CODEC_ID_H264);
    if (!codec) {
        fprintf(stderr, "codec not found\n");
        exit(1);
    }

    c = avcodec_alloc_context3(codec);
    picture = av_frame_alloc();

    if((codec->capabilities)&CODEC_CAP_TRUNCATED)
        (c->flags) |= CODEC_FLAG_TRUNCATED;

    c->height = 1080;
    c->width = 1920;

    if (avcodec_open2(c, codec, NULL) < 0) {
        fprintf(stderr, "could not open codec\n");
        exit(1);
    }

    f = fopen(filename, "rb");
    if (!f) {
        fprintf(stderr, "could not open %s\n", filename);
        exit(1);
    }

    outf = fopen(outfilename,"w");
    if(!outf){
        fprintf(stderr, "could not open %s\n", filename);
        exit(1);
    }
    frame = 0;
    for(;;) {
        avpkt.size = fread(inbuf, 1, INBUF_SIZE, f);
        if (avpkt.size == 0)
            break;

        avpkt.data = inbuf;
        while (avpkt.size > 0) {

            len = avcodec_decode_video2(c, picture, &got_picture, &avpkt);

            if (len < 0) {
                fprintf(stderr, "Error while decoding frame %d\n", frame);
                exit(1);
            }
            if (got_picture) {
                printf("saving frame %3d\n", frame);
                fflush(stdout);
                for(i=0; i<c->height; i++)
                    fwrite(picture->data[0] + i * picture->linesize[0], 1, c->width, outf  );
                for(i=0; i<c->height/2; i++)
                    fwrite(picture->data[1] + i * picture->linesize[1], 1, c->width/2, outf );
                for(i=0; i<c->height/2; i++)
                    fwrite(picture->data[2] + i * picture->linesize[2], 1, c->width/2, outf );
                frame++;
            }
            avpkt.size -= len;
            avpkt.data += len;
        }
    }

    avpkt.data = NULL;
    avpkt.size = 0;
    len = avcodec_decode_video2(c,picture, &got_picture, &avpkt);
    if(got_picture) {
        printf("saving last frame %d\n",frame);
        fflush(stdout);
        for(i=0; i<c->height; i++)
            fwrite(picture->data[0] + i * picture->linesize[0], 1, c->width, outf );
        for(i=0; i<c->height/2; i++)
            fwrite(picture->data[1] + i * picture->linesize[1], 1, c->width/2, outf );
        for(i=0; i<c->height/2; i++)
            fwrite(picture->data[2] + i * picture->linesize[2], 1, c->width/2, outf );
        frame++;
    }

    fclose(f);
    fclose(outf);

    avcodec_close(c);
    av_free(c);
    av_frame_free(&picture);
    printf("\n");
}

int main(int argc, char **argv){
    avcodec_register_all();
    video_decode("test", "trailer.264");

    return 0;
}

我也尝试了不同格式的视频(当然,在这种情况下,我更改了代码中的编解码器),例如MPEG1、H263、H265,但是没有一个能正常工作。

希望有人能帮助我,并告诉我在这里做错了什么。谢谢。


仅供参考:自2017年以来,在问题中提供的示例代码中,FF_INPUT_BUFFER_PADDING_SIZE必须替换为AV_INPUT_BUFFER_PADDING_SIZE(否则甚至无法编译)。 - mrtexaz
1个回答

4
每个输入数据包(avpkt)用于 avcodec_decode_video2 应该包含一帧完整(且仅有的)数据。也就是说,它不应在帧NAL中间被截断。因此,您的代码无法使用分块大小为 4096 字节的方式读取和发送数据。您需要通过解析 Annex B 数据并查找起始码以及分析 NAL 类型(如果帧包含多个片段,则需要更进一步的分析)来自己打包数据,或者使用 H.264 的 libavformat 解析器。作为 H.264 的解决方法,您可以尝试使用 CODEC_FLAG2_CHUNKS 标志,但我不确定其可靠性,并且仍认为 4096 字节的分块太小了。

您IP地址为143.198.54.68,由于运营成本限制,当前对于免费用户的使用频率限制为每个IP每72小时10次对话,如需解除限制,请点击左下角设置图标按钮(手机用户先点击左上角菜单按钮)。 - deadman
1
要使用libavformat,您需要使用avformat_open_input()打开文件,它可以猜测格式,或者您可以指定格式,然后使用av_read_frame()读取数据包,并将所需流的数据包发送到解码器,之后使用avformat_close_input()关闭文件。您可能还需要使用avformat_find_stream_info()获取解码器的尺寸,并使用av_find_best_stream()找到与视频流对应的AVPacket.stream_index(如果容器中有多个流)。有关更多信息,请阅读Demuxing组中的avformat.h注释。 - nobody555
我玩了一下我的h264文件(x264编码),并尝试读取NALs。 我认为它有效,我读取了前120个,我找到了起始码(00 00 00 01),第一个NAL后跟67,第二个NAL后跟68,所有其他NAL后跟01或41。01属于B帧,41属于P帧?我怎么知道哪些NAL属于一帧?为什么没有I帧?谢谢 - deadman
1
NAL起始码后的第一个字节表示字段:forbidden_zero_bit f(1),nal_ref_idc u(2),nal_unit_type u(5)。因此,0x67表示nal_ref_idc=3(最高优先级)和nal_unit_type=7(SPS)。0x68表示nal_ref_idc=3和nal_unit_type=8(PPS)。0x01表示nal_ref_idc=0(一次性/非参考优先级)和nal_unit_type=1(非IDR切片)。0x41表示nal_ref_idc=2(高/参考优先级)和nal_unit_type=1(非IDR切片)。我建议您使用libavformat,因为您已经使用了libavcodec,而不是手动解析,因为找到帧边界而不是简单的NAL边界并不容易。 - nobody555

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接