前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >FFmpeg菜鸡互啄#第3篇#视频解码

FFmpeg菜鸡互啄#第3篇#视频解码

作者头像
_gongluck
发布2018-03-08 15:37:39
1.1K0
发布2018-03-08 15:37:39
举报

解码过程

这里写图片描述
这里写图片描述

基本过程:打开输入文件,查找视频流,打开解码器,循环读帧解码帧,关闭解码器,关闭输入文件。

解码数据结构

这里写图片描述
这里写图片描述

Code

代码语言:javascript
复制
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

/*
#define __STDC_CONSTANT_MACROS
#ifndef INT64_C
#define INT64_C(c) (c ## LL)
#define UINT64_C(c) (c ## ULL)
#endif
*/

extern "C"
{
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libswscale/swscale.h"
#include "libavdevice/avdevice.h"
}

#pragma comment(lib, "avcodec.lib")
#pragma comment(lib, "avdevice.lib")
#pragma comment(lib, "avfilter.lib")
#pragma comment(lib, "avformat.lib")
#pragma comment(lib, "avutil.lib")
#pragma comment(lib, "postproc.lib")
#pragma comment(lib, "swresample.lib")
#pragma comment(lib, "swscale.lib")

#define INPUT "in.flv"
#define OUTPUT "out.yuv"

int main()
{
    int res = 0;
    int videoStream = -1;//标记视频流的编号
    char errBuf[BUFSIZ] = { 0 };
    FILE* fp_out = fopen(OUTPUT, "wb+");

    //初始化FFMPEG  调用了这个才能正常适用编码器和解码器
    av_register_all();
    printf("FFmpeg's version is: %d\n", avcodec_version());

    //FFMPEG所有的操作都要通过这个AVFormatContext来进行
    AVFormatContext* pFormatCtx = NULL;

    //打开输入视频文件
    //Open an input stream and read the header. The codecs are not opened.
    if ((res = avformat_open_input(&pFormatCtx, INPUT, NULL, NULL)) < 0)
    {
        av_strerror(res, errBuf, sizeof(errBuf));
        printf("%s\n", errBuf);
        return -1;
    }
    //Read packets of a media file to get stream information. This is useful for file formats with no headers such as MPEG.
    //相当于对输入进行 “预处理”
    avformat_find_stream_info(pFormatCtx, NULL);
    av_dump_format(pFormatCtx, 0, NULL, 0); //输出视频流的信息

    //查找流
    for (int i = 0; i < pFormatCtx->nb_streams; ++i)
    {
        if (pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO)
            videoStream = i;
    }
    if (videoStream == -1)
    {
        printf("Didn't find a video stream.\n");
        return -1;
    }

    ///查找解码器    
    AVCodecContext* pCodecCtx = pFormatCtx->streams[videoStream]->codec;
    AVCodec* pCodec = avcodec_find_decoder(pCodecCtx->codec_id);
    if (pCodec == NULL)
    {
        printf("Codec not found.\n");
        return -1;
    }

    ///打开解码器
    if (avcodec_open2(pCodecCtx, pCodec, NULL) < 0)
    {
        printf("Could not open codec.");
        return -1;
    }

    AVFrame Frame = { 0 };//不初始化,avcodec_decode_video2会报错
    AVPacket packet;
    int got_picture;
    while (1)
    {
        //读取视频帧
        //return 0 if OK, < 0 on error or end of file
        if (av_read_frame(pFormatCtx, &packet) < 0)
        {
            break; //这里认为视频读取完了
        }
        if (packet.stream_index == videoStream)
        {
            //解码视频帧
            if (avcodec_decode_video2(pCodecCtx, &Frame, &got_picture, &packet) < 0)
            {
                printf("decode error.\n");
                return -1;
            }
            if (got_picture)
            {
                if (Frame.format == PIX_FMT_YUV420P)
                {
                    //解码后YUV格式的视频像素数据保存在AVFrame的data[0]、data[1]、data[2]中。
                    //但是这些像素值并不是连续存储的,每行有效像素之后存储了一些无效像素。
                    //以亮度Y数据为例,data[0]中一共包含了linesize[0] * height个数据。
                    //但是出于优化等方面的考虑,linesize[0]实际上并不等于宽度width,而是一个比宽度大一些的值。
                    fwrite(Frame.data[0], Frame.linesize[0] * Frame.height, 1, fp_out);
                    fwrite(Frame.data[1], Frame.linesize[1] * Frame.height / 2, 1, fp_out);
                    fwrite(Frame.data[2], Frame.linesize[2] * Frame.height / 2, 1, fp_out);
                }
            }
        }
        av_free_packet(&packet);//清除packet里面指向的缓冲区
    }

    fclose(fp_out);
    avcodec_close(pCodecCtx);//关闭解码器
    avformat_close_input(&pFormatCtx);//关闭输入视频文件。avformat_free_context(pFormatCtx);就不需要了
    return 0;
}

保存的文件可以用bin目录下的YUVPlayer.exe查看,但是要设置好分辨率(Frame.linesize[0] * Frame.height)。我的30.9MB输入解码后得到一个3.8GB的数据!

这里写图片描述
这里写图片描述

注意最右边有无效数据,但是可以通过以后的转换操作去除。

Github

https://github.com/gongluck/FFmpegTest.git

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018年01月02日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 解码过程
  • 解码数据结构
  • Code
  • Github
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档