FFmpeg 代码学习example01:decode_video

 Version:FFmpeg 4.4 

Source: "example" folder

版权问题:Copyright内容截图放出

/**
 * @file
 * video decoding with libavcodec API example
 *
 * @example decode_video.c
 */

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

#include <libavcodec/avcodec.h>

#define INBUF_SIZE 4096

/* pgm_save函数 保存为pgm */
static void pgm_save(unsigned char *buf, int wrap, int xsize, int ysize,
                     char *filename)
{
    FILE *f;
    int i;
    //打开filename
    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);//关闭
}

/* decode函数 解码*/
static void decode(AVCodecContext *dec_ctx, AVFrame *frame, AVPacket *pkt,
                   const char *filename)
{
    char buf[1024];
    //ret状态码
    int ret;


    //调用avcodec_send_packet函数  将packet送到解码器
    ret = avcodec_send_packet(dec_ctx, pkt);
    if (ret < 0) {//其中一种错误
        fprintf(stderr, "Error sending a packet for decoding\n");
        exit(1);
    }

    while (ret >= 0) {
        //调用avcodec_receive_frame函数 接收解码出来的frame
        ret = avcodec_receive_frame(dec_ctx, frame);
        if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)//异常情况1
            return;
        else if (ret < 0) {//异常情况2
            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 */
        //将frame_number按格式写入buf
        snprintf(buf, sizeof(buf), "%s-%d", filename, dec_ctx->frame_number);
        //调用函数保存
        pgm_save(frame->data[0], frame->linesize[0],
                 frame->width, frame->height, buf);
    }
}


/* main函数 */
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;

    //异常处理
    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];
    outfilename = argv[2];

    //AVPacket结构体初始化
    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 MPEG-1 video decoder */
    //调用avcodec_find_decoder函数 寻找解码器
    codec = avcodec_find_decoder(AV_CODEC_ID_MPEG1VIDEO);
    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);
    }

    //调用avcodec_alloc_context3函数对 AVCodec 进行初始化
    c = avcodec_alloc_context3(codec);
    if (!c) {
        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(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);
    }

    //调用av_frame_alloc函数进行初始化
    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) {
            //调用av_parser_parse2函数解析packet
            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->size)
                //持续解码
                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;
}

 

 
posted @ 2021-04-28 22:12  Keep_Silent  阅读(35)  评论(0编辑  收藏  举报