c - AV编解码器: not able to decode hevc

标签 c ffmpeg libavcodec

我在使用 avcodec 解码 HEVC 编码视频时遇到问题。 修改源代码的一行可以解码 mpeg1 这不是我需要的。

谢谢

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

#include <libavcodec/avcodec.h>

#define INBUF_SIZE 1024

static void pgm_save(unsigned char *buf, int wrap, int xsize, int ysize,
                     char *filename)
{
    FILE *f;
    int i;

    f = fopen(filename,"wb");
    fprintf(f, "P5\n%d %d\n%d\n", xsize, ysize, 255);
    for (i = 0; i < ysize; i++)
        fwrite(buf + i * wrap, 1, xsize, f);
    fclose(f);
}

static void decode(AVCodecContext *dec_ctx, AVFrame *frame, AVPacket *pkt,
                   const char *filename)
{
    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);
    }

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

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

        /* the picture is allocated by the decoder. no need to
           free it */
        snprintf(buf, sizeof(buf), "%s-%d", filename, dec_ctx->frame_number);
        pgm_save(frame->data[0], frame->linesize[0],
                 frame->width, frame->height, buf);
    }
}

int main(int argc, char **argv)
{
    const char *filename, *outfilename;
    const AVCodec *codec;
    AVCodecParserContext *parser;
    AVCodecContext *c= NULL;
    FILE *f;
    AVFrame *frame;
    uint8_t inbuf[INBUF_SIZE + AV_INPUT_BUFFER_PADDING_SIZE];
    uint8_t *data;
    size_t   data_size;
    int ret;
    AVPacket *pkt;

    filename    = argv[1];
    outfilename = argv[2];

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

    memset(inbuf + INBUF_SIZE, 0, AV_INPUT_BUFFER_PADDING_SIZE);

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

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

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

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

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

        /* use the parser to split the data into frames */
        data = inbuf;
        while (data_size > 0) {
            ret = av_parser_parse2(parser, c, &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->data) {
                printf("NICE COCK\n");
                decode(c, frame, pkt, outfilename);

            }
        }
    }

    /* flush the decoder */
    decode(c, frame, NULL, outfilename);

    fclose(f);

    av_parser_close(parser);
    avcodec_free_context(&c);
    av_frame_free(&frame);
    av_packet_free(&pkt);

    return 0;
}

最佳答案

查看我的评论 here :

See e.g. linux.amazingdev.com/blog/archives/2011/09/28/tutorial01.c and look for the call to avformat_open_input(), av_find_stream_info () and av_read_frame() (and related code) and use that instead of av_parser_parse2(). Full API docs here: ffmpeg.org/doxygen/trunk/group__lavf__decoding.html

简而言之,您的代码用于解码,但对于典型的视频文件,您还需要一个解复用步骤。您的代码可能适用于附件 B HEVC 文件,但您的文件可能是 TS 或 MP4 或类似文件。

关于c - AV编解码器: not able to decode hevc,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71363536/

相关文章:

ffmpeg - YUV420p 中的 RGB32

为返回 size_t 的函数返回错误代码的正确方法

c - FreeBSD : Definition of BUS_TEARDOWN_INTR not found

firefox - WebM 编码文件可在 Chrome 中播放,但不能在 FFv6 HTML5-Video 中播放

javascript - 检查文件是否从 GCS 在 GCF 中的 temp 中下载

ffmpeg - "copy"在 ffmpeg 命令行中做了什么?

c - time(NULL) 和 time(&timer) 有什么区别

c - 如何在没有字符串函数的情况下将新字符串分配给字符数组

java - 从Java开始在OSX(AVFoundation)上的ffmpeg屏幕截图问题

ffmpeg - 可以在使用 libavcodec 解码期间裁剪帧大小吗?