FFmpeg关于视频解码_解码YUV实现

#include "libavutil/avutil.h"
#include "libavformat/avformat.h"

int frameCount = 0;

int decodeVideo(AVCodecContext *codecCtx, AVPacket *packet, FILE *dest_fp)
{
    int ret = avcodec_send_packet(codecCtx, packet);
    if (ret != 0)
    {
        av_log(NULL, AV_LOG_ERROR, "send packet failed: %s\n", av_err2str(ret));
        return -1;
    }
    AVFrame *frame = av_frame_alloc();
    while (avcodec_receive_frame(codecCtx, frame) == 0)
    {
        fwrite(frame->data[0], 1, codecCtx->width * codecCtx->height, dest_fp);
        fwrite(frame->data[1], 1, codecCtx->width * codecCtx->height / 4, dest_fp);
        fwrite(frame->data[2], 1, codecCtx->width * codecCtx->height / 4, dest_fp);
        frameCount++;
        av_log(NULL, AV_LOG_INFO, "frameCount: %d\n", frameCount);
    }
    if (frame)
    {
        av_frame_free(&frame);
    }
    return 0;
}

int main(int argc, char const *argv[])
{
    av_log_set_level(AV_LOG_INFO);
    if (argc < 3)
    {
        av_log(NULL, AV_LOG_ERROR, "Usage: %s <infileName> <outfileName>\n", argv[0]);
        return -1;
    }
    const char *infileName = argv[1];
    const char *outfileName = argv[2];

    AVFormatContext *inFmtCtx = NULL;

    int ret = avformat_open_input(&inFmtCtx, infileName, NULL, NULL);
    if (ret != 0)
    {
        av_log(NULL, AV_LOG_ERROR, "open input file: %s failed: %s\n", infileName, av_err2str(ret));
        return -1;
    }

    ret = avformat_find_stream_info(inFmtCtx, NULL);
    if (ret < 0)
    {
        av_log(NULL, AV_LOG_ERROR, "find input stream failed: %s\n", av_err2str(ret));
        goto fail;
    }

    ret = av_find_best_stream(inFmtCtx, AVMEDIA_TYPE_VIDEO, -1, -1, NULL, 0);
    if (ret < 0)
    {
        av_log(NULL, AV_LOG_ERROR, "find input best stream failed: %s\n", av_err2str(ret));
        goto fail;
    }

    int videoIndex = ret;

    AVCodecContext *codecCtx = avcodec_alloc_context3(NULL);
    if (codecCtx == NULL)
    {
        av_log(NULL, AV_LOG_ERROR, "alloc avcodec context failed\n");
        ret = -1;
        goto fail;
    }

    avcodec_parameters_to_context(codecCtx, inFmtCtx->streams[videoIndex]->codecpar);

    AVCodec *decoder = avcodec_find_decoder(codecCtx->codec_id);
    if (decoder == NULL)
    {
        av_log(NULL, AV_LOG_ERROR, "find decoder failed, codec_id: %d\n", codecCtx->codec_id);
        ret = -1;
        goto fail;
    }

    ret = avcodec_open2(codecCtx, decoder, NULL);
    if (ret != 0)
    {
        av_log(NULL, AV_LOG_ERROR, "open decoder failed: %s\n", av_err2str(ret));
        goto fail;
    }

    FILE *dest_fp = fopen(outfileName, "wb+");
    if (dest_fp == NULL)
    {
        av_log(NULL, AV_LOG_ERROR, "open outfile: %s failed\n", outfileName);
        ret = -1;
        goto fail;
    }

    AVPacket packet;
    av_init_packet(&packet);


    while (av_read_frame(inFmtCtx, &packet) >= 0)
    {
        if (packet.stream_index == videoIndex)
        {
            if (decodeVideo(codecCtx, &packet, dest_fp) == -1)
            {
                ret = -1;
                av_packet_unref(&packet);
                goto fail;
            }
        }
        av_packet_unref(&packet);
    }
   
    //flush decoder
    decodeVideo(codecCtx, NULL, dest_fp);

fail:
    if (inFmtCtx)
    {
        avformat_close_input(&inFmtCtx);
    }
    if (codecCtx)
    {
        avcodec_free_context(&codecCtx);
    }
    if (dest_fp)
    {
        fclose(dest_fp);
    }
    return ret;
}

 

播放:

 

 

 

先删除再编译:

已解决帧少的情况:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值