summaryrefslogtreecommitdiffstats
path: root/libSBRenc/src/env_bit.cpp
blob: 1f8c48f9d0e245cb142bd01dbe707e12d8ac4f1e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/****************************************************************************

                       (C) copyright Fraunhofer-IIS (2004)
                               All Rights Reserved

    Please be advised that this software and/or program delivery is
    Confidential Information of Fraunhofer and subject to and covered by the

    Fraunhofer IIS Software Evaluation Agreement
    between Google Inc. and  Fraunhofer
    effective and in full force since March 1, 2012.

    You may use this software and/or program only under the terms and
    conditions described in the above mentioned Fraunhofer IIS Software
    Evaluation Agreement. Any other and/or further use requires a separate agreement.


   This software and/or program is protected by copyright law and international
   treaties. Any reproduction or distribution of this software and/or program,
   or any portion of it, may result in severe civil and criminal penalties, and
   will be prosecuted to the maximum extent possible under law.

 $Id$

*******************************************************************************/
/*!
  \file
  \brief  Remaining SBR Bit Writing Routines
*/

#include "env_bit.h"
#include "cmondata.h"


#ifndef min
#define min(a,b) ( a < b ? a:b)
#endif

#ifndef max
#define max(a,b) ( a > b ? a:b)
#endif

/* ***************************** crcAdvance **********************************/
/**
 * @fn
 * @brief    updates crc data register
 * @return   none
 *
 * This function updates the crc register
 *
 */
static void crcAdvance(USHORT crcPoly,
                       USHORT crcMask,
                       USHORT *crc,
                       ULONG   bValue,
                       INT     bBits
                      )
{
  INT i;
  USHORT flag;

  for (i=bBits-1; i>=0; i--) {
    flag = ((*crc) & crcMask) ? (1) : (0) ;
    flag ^= (bValue & (1<<i)) ? (1) : (0) ;

    (*crc)<<=1;
    if(flag) (*crc) ^= crcPoly;
  }
}


/* ***************************** FDKsbrEnc_InitSbrBitstream **********************************/
/**
 * @fn
 * @brief    Inittialisation of sbr bitstream, write of dummy header and CRC
 * @return   none
 *
 *
 *
 */

INT  FDKsbrEnc_InitSbrBitstream(HANDLE_COMMON_DATA  hCmonData,
                                UCHAR              *memoryBase,      /*!< Pointer to bitstream buffer */
                                INT                 memorySize,      /*!< Length of bitstream buffer in bytes */
                                HANDLE_FDK_CRCINFO  hCrcInfo,
                                UINT                sbrSyntaxFlags)  /*!< SBR syntax flags */
{
  INT crcRegion = 0;

  /* reset bit buffer */
  FDKresetBitbuffer(&hCmonData->sbrBitbuf, BS_WRITER);

  FDKinitBitStream(&hCmonData->tmpWriteBitbuf, memoryBase,
                   memorySize, 0, BS_WRITER);

  if (sbrSyntaxFlags & SBR_SYNTAX_CRC) {
    if (sbrSyntaxFlags & SBR_SYNTAX_DRM_CRC)
    { /* Init and start CRC region */
      FDKwriteBits (&hCmonData->sbrBitbuf, 0x0, SI_SBR_DRM_CRC_BITS);
      FDKcrcInit( hCrcInfo, 0x001d, 0xFFFF, SI_SBR_DRM_CRC_BITS );
      crcRegion = FDKcrcStartReg( hCrcInfo, &hCmonData->sbrBitbuf, 0 );
    } else {
      FDKwriteBits (&hCmonData->sbrBitbuf, 0x0, SI_SBR_CRC_BITS);
    }
  }

  return (crcRegion);
}


/* ************************** FDKsbrEnc_AssembleSbrBitstream *******************************/
/**
 * @fn
 * @brief    Formats the SBR payload
 * @return   nothing
 *
 * Also the CRC will be calculated here.
 *
 */

void
FDKsbrEnc_AssembleSbrBitstream( HANDLE_COMMON_DATA  hCmonData,
                                HANDLE_FDK_CRCINFO  hCrcInfo,
                                INT                 crcRegion,
                                UINT                sbrSyntaxFlags)
{
  USHORT crcReg =  SBR_CRCINIT;
  INT numCrcBits,i;

  /* check if SBR is present */
  if ( hCmonData==NULL )
    return;

  hCmonData->sbrFillBits = 0; /* Fill bits are written only for GA streams */

  if ( sbrSyntaxFlags & SBR_SYNTAX_DRM_CRC )
  {
    /*
     * Calculate and write DRM CRC
     */
    FDKcrcEndReg( hCrcInfo, &hCmonData->sbrBitbuf, crcRegion );
    FDKwriteBits( &hCmonData->tmpWriteBitbuf, FDKcrcGetCRC(hCrcInfo)^0xFF, SI_SBR_DRM_CRC_BITS );
  }
  else
  {
    if ( !(sbrSyntaxFlags & SBR_SYNTAX_LOW_DELAY) )
    {
      /* Do alignment here, because its defined as part of the sbr_extension_data */
      int sbrLoad = hCmonData->sbrHdrBits + hCmonData->sbrDataBits;

      if ( sbrSyntaxFlags & SBR_SYNTAX_CRC ) {
        sbrLoad += SI_SBR_CRC_BITS;
      }

      sbrLoad += 4;         /* Do byte Align with 4 bit offset. ISO/IEC 14496-3:2005(E) page 39. */

      hCmonData->sbrFillBits = (8 - (sbrLoad % 8)) % 8;

      /*
        append fill bits
      */
      FDKwriteBits(&hCmonData->sbrBitbuf, 0,  hCmonData->sbrFillBits );

      FDK_ASSERT(FDKgetValidBits(&hCmonData->sbrBitbuf) % 8 == 4);
    }

    /*
      calculate crc
    */
    if ( sbrSyntaxFlags & SBR_SYNTAX_CRC ) {
      FDK_BITSTREAM  tmpCRCBuf = hCmonData->sbrBitbuf;
      FDKresetBitbuffer( &tmpCRCBuf, BS_READER );

      numCrcBits = hCmonData->sbrHdrBits + hCmonData->sbrDataBits + hCmonData->sbrFillBits;

      for(i=0;i<numCrcBits;i++){
        INT bit;
        bit = FDKreadBits(&tmpCRCBuf,1);
        crcAdvance(SBR_CRC_POLY,SBR_CRC_MASK,&crcReg,bit,1);
      }
      crcReg &= (SBR_CRC_RANGE);

      /*
       * Write CRC data.
       */
      FDKwriteBits (&hCmonData->tmpWriteBitbuf, crcReg, SI_SBR_CRC_BITS);
    }
  }

  FDKsyncCache(&hCmonData->tmpWriteBitbuf);
}