ffmpeg C语言视频解码

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qqqq245425070/article/details/86711579

视频解码生成YUV格式的原始数据

#include <stdio.h>
#include <stdlib.h>
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libswscale/swscale.h"
#include "libavutil/avutil.h"
int main(){
    //获取输入输出文件名
    char* src = "./test.mp4";
    char* desc = "./test.yuv";
    //封装格式上下文,统领全局的结构体,保存了视频文件封装格式的相关信息
    AVFormatContext * avf_ctx = avformat_alloc_context();
    int video_index = -1;
    //设置日志级别
    av_log_set_level(AV_LOG_INFO);
     //1.注册所有组件
    av_register_all();
    //2.打开视频文件
    int ret = avformat_open_input(&avf_ctx,src,NULL,NULL);
    if(ret < 0){
        av_log(NULL,AV_LOG_ERROR,"Can't open the file %s \n",src);
        return -1;
    }
    //3.获取视频文件信息
    ret = avformat_find_stream_info(avf_ctx,NULL);
    if(ret < 0){
        av_log(NULL,AV_LOG_ERROR,"can't find the stream");
        goto _end;
    }
    //打印视频文件信息
    av_dump_format(avf_ctx,0,src,0);

    for(int i = 0; i < avf_ctx->nb_streams; i++){
        // av_log(NULL,AV_LOG_INFO,"第%d次",i);
        if(avf_ctx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO){
            video_index = i;
            break;
        }
    }
    if(video_index == -1){
        av_log(NULL,AV_LOG_ERROR,"can't find the video stream");
        goto _end;
    }

    //只有知道视频的编码方式,才能够根据编码方式去找到解码器
    //获取视频流中的编解码上下文
    AVCodecContext* avc_cxt = avf_ctx->streams[video_index]->codec;

    //根据视频中的编码Id找到对应的解码器
    AVCodec *avc = avcodec_find_decoder(avc_cxt->codec_id);
    if(avc == NULL){
        av_log(NULL,AV_LOG_ERROR,"can't find the decoder");
        goto _end;
    }

    //  打开解码器
    ret = avcodec_open2(avc_cxt,avc,NULL);
    if(ret < 0){
        av_log(NULL,AV_LOG_ERROR,"can't open the codec");
        goto _end;
    }

    // 输出视频信息
    av_log(NULL,AV_LOG_INFO,"视频格式: %s \n",avf_ctx->iformat->name);
    av_log(NULL,AV_LOG_INFO,"视频时长: %lld s\n",avf_ctx->duration/1000000);
    av_log(NULL,AV_LOG_INFO,"视频宽x高: %d x %d \n",avc_cxt->width,avc_cxt->height);
    av_log(NULL,AV_LOG_INFO,"视频的解码器的名称:%s \n",avc->name);
    //准备读取
    // AVPacket用于存储一帧一帧的压缩数据(H264)
    //缓冲区,开辟空间
    AVPacket * avp = (AVPacket*)av_malloc(sizeof(AVPacket));

    //AVFrame用于存储解码后的像素数据(YUV)
    //内存分配
    AVFrame * avframe = av_frame_alloc();
    //YUV420
    AVFrame *pFrameYUV = av_frame_alloc();
    //只有指定了AVFrame的像素格式、画面大小才能真正分配内存
    //缓冲区分配内存
    int8_t *buf = (int8_t*)av_malloc(avpicture_get_size(AV_PIX_FMT_YUV420P,avc_cxt->width,avc_cxt->height));

    //初始化缓冲区
    avpicture_fill((AVPicture*)pFrameYUV , buf ,AV_PIX_FMT_YUV420P , avc_cxt->width,avc_cxt->height);
    //用于转码(缩放)的参数,转之前的宽高,转之后的宽高,格式等    
    struct SwsContext *sws_ctx = sws_getContext(avc_cxt->width,avc_cxt->height,avc_cxt->pix_fmt,
                                                avc_cxt->width,avc_cxt->height,AV_PIX_FMT_YUV420P,
                                                SWS_BICUBIC,NULL,NULL,NULL);

    int got_pic;
    FILE *out_yuv = fopen(desc,"wb+");                                      
    int frame_count = 0; 
    while(av_read_frame(avf_ctx,avp) >= 0){
        //只要视频压缩数据(根据流的索引位置判断)
        if(avp->stream_index == video_index){
            ret = avcodec_decode_video2(avc_cxt,avframe,&got_pic,avp);
            if(ret < 0){
                av_log(NULL,AV_LOG_ERROR,"decode is error");
                goto _end;
            }
            //如果got_pic == 0 表示解码完成  非0表示正在解码
            if(got_pic){
                //AVFrame转为像素格式YUV420,宽高
                //2 6输入、输出数据
                //3 7输入、输出画面一行的数据的大小 AVFrame 转换是一行一行转换的
                //4 输入数据第一列要转码的位置 从0开始
                //5 输入画面的高度
                sws_scale(sws_ctx,avframe->data,avframe->linesize,0,avc_cxt->height,pFrameYUV->data,pFrameYUV->linesize);
                // sws_scale(sws_ctx, pFrame->data, pFrame->linesize, 0, pCodecCtx->height,
                //           pFrameYUV->data, pFrameYUV->linesize);
                 //输出到YUV文件
                //AVFrame像素帧写入文件
                //data解码后的图像像素数据(音频采样数据)
                //Y 亮度 UV 色度(压缩了) 人对亮度更加敏感
                //U V 个数是Y的1/4
                int y_size = avc_cxt->width * avc_cxt->height;
                fwrite(pFrameYUV->data[0], 1 ,y_size,out_yuv);
                fwrite(pFrameYUV->data[1], 1 ,y_size / 4,out_yuv);
                fwrite(pFrameYUV->data[2], 1 ,y_size / 4,out_yuv);
                frame_count++;
                av_log(NULL,AV_LOG_INFO,"解析完第%d帧\n",frame_count);
            }

        }
        //释放avpacket
        av_free_packet(avp);
    }
    _end:
    fclose(out_yuv);
    av_frame_free(&avframe);
    if(avc_cxt != NULL){
         avcodec_close(avc_cxt);
    }
    avformat_free_context(avf_ctx);
    return 0;
}

用ffplay播放YUV数据的视频 ffplay -video_size 1280x720 out.yuv(这个宽高一定是转换为yuv数据的宽高,不然视频播放会有问题)

猜你喜欢

转载自blog.csdn.net/qqqq245425070/article/details/86711579