h264解码之哥伦布指数编码封装

免责声明:以下仅为个人学习记录,如有错误或其他建议,烦请指出,不胜感激

1、目的

为了不依赖于其他的三方库模块,独立得解码h264得码流,根据h264官方文档,需要对哥伦布指数编码进行封装,下面得程序在性能上肯定不如其他得三方库,但是胜在方便理解,有关哥伦布指数编码得相关知识,请查阅百度,这里不再赘述。

2、代码展示

首先定义接口

// com_columbus.h

#ifndef COM_COLUMBUS_H
#define COM_COLUMBUS_H

#include "com_def.h"

typedef enum
{
	BITSTREAM_OUT_OF_BOUNDARY = -1,
	BITSTREAM_BITS_NUM_INVALID = -2
}BitStreamErrno;

class IBitStream
{
public:
	static IBitStream* CreateInstance();

	virtual ~IBitStream() {}

	virtual void Init(uint8_t* data, uint32_t len) = 0;

	virtual uint32_t GetBits(uint16_t n) = 0;

	virtual uint32_t GetUe() = 0;

	virtual uint32_t GetSe() = 0;
};

#endif

然后需要对这个接口进行实现,这里是具体得实现类

// com_columbus_impl.h

#ifndef COM_COLUMBUS_IMPL_H
#define COM_COLUMBUS_IMPL_H

#include "com_columbus.h"

class BitStreamImpl :public IBitStream
{
    public:
    virtual void Init(uint8_t* data, uint32_t len);

    virtual uint32_t GetU1();

    virtual uint32_t GetBits(uint16_t n);

    virtual uint32_t GetUe();

    virtual uint32_t GetSe();

    private:
    uint8_t* start;
    uint8_t* end;
    uint8_t* p;
    uint8_t left_bits;
};


#endif

下面是具体得实现定义

#include "com_columbus_impl.h"

IBitStream* IBitStream::CreateInstance()
{
	return new BitStreamImpl;
}

void BitStreamImpl::Init(uint8_t* data, uint32_t len)
{
    start = data;
    end = data + len;
    p = start;
    left_bits = 8;
}

uint32_t BitStreamImpl::GetU1()
{
    uint32_t res = 0;
    if (p >= end)
    {
        throw BITSTREAM_OUT_OF_BOUNDARY;
    }
    left_bits--;
    res = ((*p) >> left_bits) & 0x01;
    if (left_bits == 0)
    {
        left_bits = 8;
        p++;
    }
    return res;
}

uint32_t BitStreamImpl::GetBits(uint16_t n)
{
    if (n > 32)
    {
        throw BITSTREAM_BITS_NUM_INVALID;
    }
    if (n == 0)
    {
        return 0;
    }

    uint32_t res = 0;
    res |= GetU1();
    for (int i = 1; i < n; i++)
    {
        res <<= 1;
        res |= GetU1();
    }
    return res;
}

uint32_t BitStreamImpl::GetUe()
{
    int cnt = 0;
    while (1)
    {
        if (GetU1() || cnt > 32)
        {
            break;
        }
        cnt++;
    }
    return (GetBits(cnt) | (1 << cnt)) - 1;
}

uint32_t BitStreamImpl::GetSe()
{
    uint32_t res = GetUe();
    if (res & 0x01 == 0)
    {
        res >>= 1;
        res = 0 - res;
        return res;
    }
    return (res + 1) >> 1;
}


在使用方面得话,因为里面有异常处理,因此需要在调用得时候try catch一下

#include "com_columbus.h"

// 这里仅演示方法,对于编造得数据是否正常不做考虑
const char* g_data = "123456789";
const int g_len = 9;
int main()
{
    IBitStream* btstream = NULL;
    btstream = IBitStream::CreateInstance();
    if(btstream)
    {
        btstream.Init(g_data,g_len);
        try{
            btstream.GetBits(8);
            btstream.GetBits(4);
            btstream.GetBits(2);
            btstream.GetBits(1);
            btstream.GetUe(1);
            btstream.GetSe(1);
        }catch(BitStreamErrno errno)
        {
            if(BITSTREAM_OUT_OF_BOUNDARY == errno)
            {
                // 超过字节边界
            }
            if(BITSTREAM_BITS_NUM_INVALID == errno)
            {
                //比特个数非法
            }
        }
    }
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值