OpenCores
URL https://opencores.org/ocsvn/funbase_ip_library/funbase_ip_library/trunk

Subversion Repositories funbase_ip_library

[/] [funbase_ip_library/] [trunk/] [TUT/] [ip.application/] [h.263_encoder_main/] [1.0/] [src/] [quantize.c] - Blame information for rev 145

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 145 lanttu
/*---------------------------------------------------------------------------
2
*
3
*  Course:   System Design II
4
*
5
*  Module:   quantize.c
6
*
7
*  Purpose:  Quantization functions for H.263 encoder
8
*
9
*  Notes:    Only INTRA MB:s supported
10
*
11
*  Author:   Tero Kangas and Olli Lehtoranta
12
*
13
*  History:  28/10/2002: + Original version ready
14
*
15
**---------------------------------------------------------------------------
16
*/
17
#include "headers.h"
18
 
19
/*---------------------------------------------------------------------------
20
*
21
*  Function: quantizeIntraMB
22
*
23
*  Input:    QP = H.263 quantization parameter
24
*            MB = pointer to macroblock data
25
*
26
*  Return:   none
27
*
28
*  Purpose:  Correct quantization for Intra macroblock. The DC
29
*            coefficients are processed separately. Also sets the coded
30
*            block pattern for given macroblock.
31
*
32
**---------------------------------------------------------------------------
33
*/
34
void quantizeIntraMB( const sint32 QP, MBType * const MB ) {
35
 
36
  sint32 block, i;
37
  sint32 dc;
38
  sint32 cbp;
39
  sint32 qLevel;
40
  sint32 origCoeff;
41
  sint32 twiceTheQP = QP*2;
42
  sint32 zeroBlockIndication = 0;
43
  sint16 *pMB;
44
 
45
  cbp = 0;
46
  pMB = MB->data;
47
 
48
  for( block=0; block<6; block++ ) {
49
 
50
    /* Special quantization for DC coefficient  */
51
    dc = *pMB;
52
    dc = dc / 8;
53
    CLIP(1,dc,254);
54
    *(pMB++) = (sint16)dc;
55
 
56
    #ifdef ENC_DEBUG
57
    printf("o: %x ", (uint16)dc);
58
    #endif
59
 
60
 
61
    for(i=1; i<64; i++ ) {
62
      origCoeff = *pMB;
63
      qLevel = abs(origCoeff) / twiceTheQP;
64
      if( qLevel > 127 )
65
          qLevel = 127;
66
 
67
      zeroBlockIndication = zeroBlockIndication | qLevel;
68
 
69
      if( origCoeff < 0 )
70
          qLevel = -qLevel;
71
 
72
      *(pMB++) = (sint16)qLevel;
73
 
74
      #ifdef ENC_DEBUG
75
      printf("%x ", (uint16)qLevel);
76
      #endif
77
    }
78
 
79
    #ifdef ENC_DEBUG
80
    printf("\r\n");
81
    #endif
82
 
83
    if( zeroBlockIndication != 0 ){
84
      /* Set block pattern accordingly */
85
      cbp = cbp | ( 1 << ( 5 - block ) );
86
      zeroBlockIndication = 0;
87
    }
88
  }
89
 
90
  MB->cbp = cbp;
91
}
92
 
93
/*---------------------------------------------------------------------------
94
*
95
*  Function: quantizeIntraMBInverse
96
*
97
*  Input:    QP = H.263 quantization parameter
98
*            MB = pointer to macroblock data
99
*
100
*  Return:   none
101
*
102
*  Purpose:  Inverse quantization of Intra macroblock. DC values are processed
103
*            separately. In addition, no inverse quantization is
104
*            performed if coded block pattern says that there are no
105
*            AC coefficients present. In this case the 8 x 8 matrix
106
*            is filled with zeros (Simplification of computations).
107
*
108
**---------------------------------------------------------------------------
109
*/
110
void quantizeIntraMBInverse( const sint32 QP, MBType * const MB ){
111
 
112
  sint32 block, i;
113
  sint32 reconCoeff;
114
  sint32 origCoeff;
115
  sint32 bias;
116
  sint32 mask = 32;
117
  sint16 *pMB;
118
 
119
  /* Odd/even determination and inverse quantization formula bias  */
120
  bias = 0;
121
  if( ( QP & 1 ) == 0 )
122
      bias = 1;
123
 
124
  pMB = MB->data;
125
 
126
  for( block = 0; block < 6; block++ ) {
127
    /* DC coeff */
128
    *(pMB++) = (sint16)(8 * MB->data[64*block]);
129
 
130
    /* The inverse quantization for AC coeffs is needed
131
       on if there are nonzero coefficients in MB */
132
    if( ( MB->cbp & mask ) != 0 ){
133
      /* The were nonzero AC-coefficients */
134
      for( i=1; i<64; i++ ) {
135
          origCoeff = *pMB;
136
          reconCoeff = QP * ( 2 * abs(origCoeff) + 1 ) - bias;
137
          if( origCoeff < 0 )
138
              reconCoeff = -reconCoeff;
139
          else if( origCoeff == 0 )
140
              reconCoeff = 0;
141
          *(pMB++) = (sint16)reconCoeff;
142
      }
143
    } else {
144
      /* All AC coeffs are zero -> nothing needs to be done */
145
    }
146
    mask = mask >> 1;
147
  }
148
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.