www.pudn.com > avs-m3.rar > nal.c


 
/*! 
 ************************************************************************ 
 * \file  nal.c 
 * \brief  
 *     NAL unit handling 
 ************************************************************************ 
 */ 
 
#include  
#include  
//#include  
 
#include "global.h" 
#include "nalu.h" 
#include "memalloc.h" 
 
 
/*!  
 ************************************************************************************* 
 * \brief 
 *    Allocates memory for a NALU 
 * 
 * \param buffersize 
 *     size of NALU buffer  
 * 
 * \return 
 *    pointer to a NALU 
 ************************************************************************************* 
 */ 
  
 
NALU_t *AllocNALU(int buffersize) 
{ 
  NALU_t *n; 
 
  if ((n = (NALU_t*)calloc (1, sizeof (NALU_t))) == NULL) no_mem_exit ("AllocNALU: n"); 
 
  if ((n->buf = (byte*)calloc (buffersize, sizeof (byte))) == NULL) no_mem_exit ("AllocNALU: n->buf"); 
   
  return n; 
} 
 
 
/*!  
 ************************************************************************************* 
 * \brief 
 *    Frees a NALU 
 * 
 * \param n  
 *    NALU to be freed 
 * 
 ************************************************************************************* 
 */ 
 
void FreeNALU(NALU_t *n) 
{ 
  if (n) 
  { 
    if (n->buf) 
    { 
      free(n->buf); 
      n->buf=NULL; 
    } 
    free (n); 
  } 
} 
 
 
/*!  
 ************************************************************************************* 
 * \brief 
 *    Converts a NALU to an RBSP 
 * 
 * \param  
 *    nalu: nalu structure to be filled 
 * 
 * \return 
 *    length of the RBSP in bytes 
 ************************************************************************************* 
 */ 
int NALUtoRBSP (NALU_t *nalu) 
{ 
  assert (nalu != NULL); 
 
 
  nalu->len = EBSPtoRBSP (nalu->buf, nalu->len, 1) ;  
 
  return nalu->len ; 
} 
 
/*! 
************************************************************************ 
* \brief 
*    Converts Encapsulated Byte Sequence Packets to RBSP 
* \param streamBuffer 
*    pointer to data stream 
* \param end_bytepos 
*    size of data stream 
* \param begin_bytepos 
*    Position after beginning  
************************************************************************/ 
 
 
int EBSPtoRBSP(byte *streamBuffer, int end_bytepos, int begin_bytepos) 
{ 
  int i, j, count; 
  count = 0; 
   
  if(end_bytepos < begin_bytepos) 
    return end_bytepos; 
   
  j = begin_bytepos; 
   
  for(i = begin_bytepos; i < end_bytepos; i++)  
  { //starting from begin_bytepos to avoid header information 
 
    streamBuffer[j] = streamBuffer[i]; 
 
    j++; 
  } 
   
  return j; 
}