您的位置:首页 > 其它

JM8.6中的一个重要结构体NALU_t的定义、分配和释放

2012-11-24 23:34 323 查看
       在JM8.6中,NALU_t是一个非常重要的结构体,回寝室之前,欣赏一下结构体NALU_t的定义:

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 nal_unit_type;            //! NALU_TYPE_xxxx
int nal_reference_idc;        //! NALU_PRIORITY_xxxx
int forbidden_bit;            //! should be always FALSE
byte *buf;        //! conjtains the first byte followed by the EBSP
} NALU_t;


     接着欣赏一下AllocNALU函数

NALU_t *AllocNALU(int buffersize)
{
NALU_t *n;

if ((n = (NALU_t*)calloc (1, sizeof (NALU_t))) == NULL) no_mem_exit ("AllocNALU: n");  // 分配一个结构体的空间

n->max_size=buffersize;

if ((n->buf = (byte*)calloc (buffersize, sizeof (byte))) == NULL) no_mem_exit ("AllocNALU: n->buf"); // 也要分配这个空间

return n;
}

      

      接着欣赏FreeNALU函数.

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


     欣赏完最后一个近似模拟程序就回去啦,如下:

#include<iostream>
using namespace std;

typedef struct nalu
{
int bufferSize;
int *buf;
}NALU_t;

NALU_t* allocNALU(int size)
{
NALU_t *n = new NALU_t;
n->bufferSize = size;
n->buf = new int[n->bufferSize];

return n;
}

void freeNALU(NALU_t *n)
{
if(NULL != n)
{
if(NULL != n->buf)
{
free(n->buf);
n->buf = NULL;
}
free(n);
n->buf = NULL;
}
n->bufferSize = 0;
}

int main()
{
int size = 280;
NALU_t *n = allocNALU(size);

int i;
for(i = 0; i < size; i++)
{
n->buf[i] = i;
}

for(i = 0; i < size; i++)
{
cout << n->buf[i] << endl;
}

freeNALU(n);

return 0;
}


      Ok, 回去.

 

      第二天了. 发现还有几个函数不错呢,可以看看:

static byte *NAL_Payload_buffer;  // 文件全局变量
void FreeNalPayloadBuffer()
{
if(NAL_Payload_buffer)
{
free(NAL_Payload_buffer);
NAL_Payload_buffer=NULL;
}
}
void AllocNalPayloadBuffer() // 分配一帧的堆
{
const int buffer_size = (input->img_width * input->img_height * 4); // AH 190202: There can be data expansion with
// low QP values. So, we make sure that buffer
// does not everflow. 4 is probably safe multiplier.
FreeNalPayloadBuffer();

NAL_Payload_buffer = (byte *) calloc(buffer_size, sizeof(byte));
assert (NAL_Payload_buffer != NULL);
}


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐