ffmpeg解码h264文件,opencv显示

H264.h

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

typedef struct
{
    int startcodeprefix_len;      //! 4 for parameter sets and first slice in picture, 3 for everything else (suggested)
    unsigned len;                 //! Length of the NAL unit (Excluding the start code, which does not belong to the NALU)
    unsigned max_size;            //! Nal Unit Buffer size
    int forbidden_bit;            //! should be always FALSE
    int nal_reference_idc;        //! NALU_PRIORITY_xxxx
    int nal_unit_type;            //! NALU_TYPE_xxxx    
    char *buf;                    //! contains the first byte followed by the EBSP
    unsigned short lost_packets;  //! true, if packet loss is detected
} NALU_t;

int GetNalu();
void FreeNALU(NALU_t *n);
NALU_t *AllocNALU(int buffersize);
void OpenBitstreamFile(char *fn);
int GetAnnexbNALU(NALU_t *nalu);
void dump(NALU_t *nal);
int DumpChar(char * filename, char * buf, int len);
FILE * getFile();

H264.cpp

// rtspSend.cpp : Defines the entry point for the console application.
//


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <memory.h>
#include "h264.h"
static char* dumpRoot = ".\\dump\\";
static char file2open[1024];
#define  UDP_MAX_SIZE 1400



FILE *bits = NULL;                //!< the bit stream file
static int FindStartCode2(unsigned char *Buf);//查找开始字符0x000001
static int FindStartCode3(unsigned char *Buf);//查找开始字符0x00000001

// 
 static int info2=0, info3=0;
// RTP_FIXED_HEADER *rtp_hdr;

// NALU_HEADER      *nalu_hdr;
// FU_INDICATOR *fu_ind;
// FU_HEADER        *fu_hdr;

FILE * getFile()
{
    return bits;
}

//为NALU_t结构体分配内存空间
NALU_t *AllocNALU(int buffersize)
{
    NALU_t *nal =NULL;

    if ((nal = (NALU_t*)calloc (1, sizeof (NALU_t))) == NULL)
    {
        printf("AllocNALU: n");
        exit(0);
    }

    nal->max_size=buffersize;

    if ((nal->buf = (char*)calloc (buffersize, sizeof (char))) == NULL)
    {
        free (nal);
        printf ("AllocNALU: nal->buf");
        exit(0);
    }

    return nal;
}

//释放
void FreeNALU(NALU_t *n)
{
    if (n)
    {
        if (n->buf)
        {
            free(n->buf);
            n->buf=NULL;
        }
        free (n);
    }
}

void OpenBitstreamFile (char *fn)
{
    if (NULL == (bits=fopen(fn, "rb")))
    {
        printf("open file error\n");
        exit(0);
    }
}

//这个函数输入为一个NAL结构体,主要功能为得到一个完整的NALU并保存在NALU_t的buf中,
//获取他的长度,填充F,IDC,TYPE位。
//并且返回两个开始字符之间间隔的字节数,即包含有前缀的NALU的长度
int GetAnnexbNALU (NALU_t *nalu)
{
    int pos = 0;
    int StartCodeFound, rewind;
    unsigned char *Buf;

    if ((Buf = (unsigned char*)calloc (nalu->max_size , sizeof(char))) == NULL) 
        printf ("GetAnnexbNALU: Could not allocate Buf memory\n");

    nalu->startcodeprefix_len=3;//初始化码流序列的开始字符为3个字节

    if (3 != fread (Buf, 1, 3, bits))//从码流中读3个字节
    {
        free(Buf);
        return 0;
    }
    info2 = FindStartCode2 (Buf);//判断是否为0x000001 
    if(info2 != 1) 
    {
        //如果不是,再读一个字节
        if(1 != fread(Buf+3, 1, 1, bits))//读一个字节
        {
            free(Buf);
            return 0;
        }
        info3 = FindStartCode3 (Buf);//判断是否为0x00000001
        if (info3 != 1)//如果不是,返回-1
        { 
            free(Buf);
            return -1;
        }
        else 
        {
            //如果是0x00000001,得到开始前缀为4个字节
            pos = 4;
            nalu->startcodeprefix_len = 4;
        }
    }
    else
    {
        //如果是0x000001,得到开始前缀为3个字节
        nalu->startcodeprefix_len = 3;
        pos = 3;
    }
    //查找下一个开始字符的标志位
    StartCodeFound = 0;
    info2 = 0;
    info3 = 0;

    while (!StartCodeFound)
    {
        if (feof (bits))//判断是否到了文件尾
        {
            nalu->len = (pos-1)-nalu->startcodeprefix_len;
            memcpy (nalu->buf, &Buf[nalu->startcodeprefix_len], nalu->len);     
            nalu->forbidden_bit = nalu->buf[0] & 0x80; //1 bit
            nalu->nal_reference_idc = nalu->buf[0] & 0x60; // 2 bit
            nalu->nal_unit_type = (nalu->buf[0]) & 0x1f;// 5 bit
            free(Buf);
            return pos-1;
        }
        Buf[pos++] = fgetc (bits);//读一个字节到BUF中
        info3 = FindStartCode3(&Buf[pos-4]);//判断是否为0x00000001
        if(info3 != 1)
            info2 = FindStartCode2(&Buf[pos-3]);//判断是否为0x000001
        StartCodeFound = (info2 == 1 || info3 == 1);
    }

    // Here, we have found another start code (and read length of startcode bytes more than we should
    // have.  Hence, go back in the file
    rewind = (info3 == 1)? -4 : -3;

    if (0 != fseek (bits, rewind, SEEK_CUR))//把文件指针指向前一个NALU的末尾
    {
        free(Buf);
        printf("GetAnnexbNALU: Cannot fseek in the bit stream file");
    }

    // Here the Start code, the complete NALU, and the next start code is in the Buf.  
    // The size of Buf is pos, pos+rewind are the number of bytes excluding the next
    // start code, and (pos+rewind)-startcodeprefix_len is the size of the NALU excluding the start code

    nalu->len = (pos+rewind)-nalu->startcodeprefix_len;
    memcpy (nalu->buf, &Buf[nalu->startcodeprefix_len], nalu->len);//拷贝一个完整NALU,不拷贝起始前缀0x000001或0x00000001
    nalu->forbidden_bit = nalu->buf[0] & 0x80;      //1 bit
    nalu->nal_reference_idc = nalu->buf[0] & 0x60;  //2 bit
    nalu->nal_unit_type = (nalu->buf[0]) & 0x1f;    //5 bit
    free(Buf);

    return (pos+rewind);//返回两个开始字符之间间隔的字节数,即包含有前缀的NALU的长度
}

static int FindStartCode2 (unsigned char *Buf)
{
    if(Buf[0]!=0 || Buf[1]!=0 || Buf[2] !=1) 
        return 0; //判断是否为0x000001,如果是返回1
    else 
        return 1;
}

static int FindStartCode3 (unsigned char *Buf)
{
    if(Buf[0]!=0 || Buf[1]!=0 || Buf[2] !=0 || Buf[3] !=1) 
        return 0;//判断是否为0x00000001,如果是返回1
    else 
        return 1;
}

int rtpnum = 0;

//输出NALU长度和TYPE
void dump(NALU_t *nal)
{
    if (!nal) 
        return;
    printf("%3d, len: %6d  ",rtpnum++, nal->len);
    printf("nal_unit_type: %x\n", nal->nal_unit_type);
}

int DumpChar(char * filename, char * buf, int len)
{
    FILE* file;
    int w, h;
    unsigned char * temp = (unsigned char *)buf;
    sprintf(file2open, "%s%s", dumpRoot, filename);
    int mHeight = 0;
    int mWidth = 100;
    int mYu = 0;
    mHeight = len / 100;
    mYu = len % 100;
    file = fopen(file2open, "w+");
    for (h = 0; h < mHeight; h++)
    {
        for (w = 0; w < mWidth - 1; w++)
        {
            fprintf_s(file, "%3x,", temp[h * mWidth + w]);
        }
        fprintf_s(file, "%3x\n", temp[h * mWidth + w]);
    }

    for (w = 0; w < mYu - 1; w++)
    {
        fprintf_s(file, "%3x,", temp[h * mWidth + w]);
    }
    fprintf_s(file, "%3x\n", temp[h * mWidth + w]);
    fclose(file);
    return 0;
}

Nalu2BGR.h

extern "C"
{
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libswscale/swscale.h"
#include "libavutil/avutil.h" 
};

typedef struct
{
    AVCodec         *pCodec = NULL;
    AVCodecContext  *pCodecCtx = NULL;
    SwsContext      *img_convert_ctx = NULL;
    AVFrame         *pFrame = NULL;
    AVFrame         *pFrameBGR = NULL;

    int first;
    unsigned char * outBuffer;

}Nalu2BGR_Info;

Nalu2BGR_Info * H264_Init(void);
int H264_2_RGB(Nalu2BGR_Info * pNalu2BGR_Info, char *inputbuf, int frame_size, unsigned char *outputbuf, unsigned int*outsize);
void H264_Release(Nalu2BGR_Info * pNalu2BGR_Info);

Nalu2BGR.cpp

#include "Nalu2BGR.h"

Nalu2BGR_Info * H264_Init(void)
{
    Nalu2BGR_Info * pNalu2BGR_Info = (Nalu2BGR_Info *)calloc(1, sizeof(Nalu2BGR_Info));
    /* register all the codecs */
    avcodec_register_all();

    /* find the h264 video decoder */
    pNalu2BGR_Info->pCodec = avcodec_find_decoder(AV_CODEC_ID_H264);
    if (!pNalu2BGR_Info->pCodec) {
        fprintf(stderr, "codec not found\n");
    }
    pNalu2BGR_Info->pCodecCtx = avcodec_alloc_context3(pNalu2BGR_Info->pCodec);

    //初始化参数,下面的参数应该由具体的业务决定  
    pNalu2BGR_Info->pCodecCtx->time_base.num = 1;
    pNalu2BGR_Info->pCodecCtx->frame_number = 1; //每包一个视频帧  
    pNalu2BGR_Info->pCodecCtx->codec_type = AVMEDIA_TYPE_VIDEO;
    pNalu2BGR_Info->pCodecCtx->bit_rate = 0;
    pNalu2BGR_Info->pCodecCtx->time_base.den = 30;//帧率  
    pNalu2BGR_Info->pCodecCtx->width = 640;//视频宽  
    pNalu2BGR_Info->pCodecCtx->height = 480;//视频高  

    /* open the coderc */
    if (avcodec_open2(pNalu2BGR_Info->pCodecCtx, pNalu2BGR_Info->pCodec, NULL) < 0) {
        fprintf(stderr, "could not open codec\n");
    }
    // Allocate video frame  
    //pNalu2BGR_Info->pFrame = new AVFrame[640 * 480 * 3];
    pNalu2BGR_Info->pFrame = av_frame_alloc();
    if (pNalu2BGR_Info->pFrame == NULL)
        return NULL;
    // Allocate an AVFrame structure  
    //pNalu2BGR_Info->pFrameBGR = new AVFrame[640 * 480 * 3];
    pNalu2BGR_Info->pFrameBGR = av_frame_alloc();
    if (pNalu2BGR_Info->pFrameBGR == NULL)
        return NULL;
    pNalu2BGR_Info->first = 0;
    pNalu2BGR_Info->outBuffer = NULL;
    return pNalu2BGR_Info;

}

int H264_2_RGB(Nalu2BGR_Info * pNalu2BGR_Info, char *inputbuf, int frame_size, unsigned char *outputbuf, unsigned int*outsize)
{
    uint8_t         *buffer = NULL;

    AVCodec         *pCodec = pNalu2BGR_Info->pCodec;
    AVCodecContext  *pCodecCtx = pNalu2BGR_Info->pCodecCtx;

    AVFrame         *pFrame = pNalu2BGR_Info->pFrame;
    AVFrame         *pFrameBGR = pNalu2BGR_Info->pFrameBGR;

    printf("Video decoding\n");
    int ret, got_picture;
    AVPacket packet;
    av_init_packet(&packet);
    packet.size = frame_size;
    packet.data = (uint8_t *)inputbuf;

    ret = avcodec_decode_video2(pCodecCtx, pFrame, &got_picture, &packet);
    if (ret < 0)
    {
        printf("Decode Error. ret = %d(解码错误)\n", ret);
        return -1;
    }
    if (pNalu2BGR_Info->first == 0)
    {
        pNalu2BGR_Info->outBuffer = (uint8_t *)av_malloc(avpicture_get_size(AV_PIX_FMT_BGR24, pCodecCtx->width, pCodecCtx->height));
        avpicture_fill((AVPicture *)pFrameBGR, pNalu2BGR_Info->outBuffer, AV_PIX_FMT_BGR24, pCodecCtx->width, pCodecCtx->height);
        pNalu2BGR_Info->first = 1;
        pNalu2BGR_Info->img_convert_ctx = sws_getContext(pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height, AV_PIX_FMT_BGR24, SWS_BICUBIC, NULL, NULL, NULL);

    }
    sws_scale(pNalu2BGR_Info->img_convert_ctx, (const uint8_t* const*)pFrame->data, pFrame->linesize, 0, pCodecCtx->height, pFrameBGR->data, pFrameBGR->linesize);
    memcpy(outputbuf, pFrameBGR->data[0], pCodecCtx->width * pCodecCtx->height * 3);
    *outsize = pCodecCtx->width * pCodecCtx->height * 3;

    return 0;
}
void H264_Release(Nalu2BGR_Info * pNalu2BGR_Info)
{
    avcodec_close(pNalu2BGR_Info->pCodecCtx);
    av_free(pNalu2BGR_Info->pCodecCtx);
    av_free(pNalu2BGR_Info->pFrame);
    av_free(pNalu2BGR_Info->pFrameBGR);
    free(pNalu2BGR_Info->outBuffer);
}

main.cpp

#include "opencv2/opencv.hpp"
#include "Nalu2BGR.h"
#include "H264.h"


int main(int argc, char* argv[])
{
    OpenBitstreamFile("./src01.h264");
    NALU_t *nal;
    char fName[300];
    int Frame = 0;
    nal = AllocNALU(8000000);//为结构体nalu_t及其成员buf分配空间。返回值为指向nalu_t存储空间的指针

    Nalu2BGR_Info * pNalu2BGR_Info = H264_Init();

    unsigned char *outputbuf = (unsigned char *)calloc(1000 * 1000, sizeof(char));
    unsigned int outsize = 0;

    unsigned char *m_pData = (unsigned char *)calloc(1000 * 1000, sizeof(char));

    int sizeHeBing = 0;
    while (!feof(getFile()))
    {
        GetAnnexbNALU(nal);//每执行一次,文件的指针指向本次找到的NALU的末尾,
                           //下一个位置即为下个NALU的起始码0x000001
        dump(nal);//输出NALU长度和TYPE

        sprintf(fName, "dump[Len=%d][%d].txt", nal->len, Frame);

        memset(m_pData, 0, 4);
        m_pData[3] = 1;
        memcpy(m_pData + 4, nal->buf, nal->len);
        sizeHeBing = nal->len + 4;
        Frame++;

        int ret = H264_2_RGB(pNalu2BGR_Info, (char *)m_pData, sizeHeBing, outputbuf, &outsize);
        if (ret != 0)
            continue;

        cv::Mat  image = cv::Mat(pNalu2BGR_Info->pCodecCtx->height, pNalu2BGR_Info->pCodecCtx->width, CV_8UC3);

        memcpy(image.data, outputbuf, pNalu2BGR_Info->pCodecCtx->height * pNalu2BGR_Info->pCodecCtx->width * 3);

        cv::imshow("xxx", image);

        cv::waitKey(40);


    }

    FreeNALU(nal);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/baidu_31872269/article/details/78575743