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/] [code.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:   code.c
6
*
7
*  Purpose:  Run-Length Encoding functions for H.263 encoder
8
*
9
*  Notes:    Only INTRA MB:s supported
10
*
11
*  Author:   Tero Kangas and Olli Lehtoranta
12
*
13
*  History:  31/10/2002: + Original version ready
14
*
15
**---------------------------------------------------------------------------
16
*/
17
#include "headers.h"
18
 
19
 
20
const uint16 zigzagScan[64] =
21
{
22
  0,1,8,16,9,2,3,10,17,24,32,25,18,11,4,5,
23
  12,19,26,33,40,48,41,34,27,20,13,6,7,14,21,28,
24
  35,42,49,56,57,50,43,36,29,22,15,23,30,37,44,51,
25
  58,59,52,45,38,31,39,46,53,60,61,54,47,55,62,63
26
};
27
 
28
 
29
/*---------------------------------------------------------------------------
30
*
31
*  Function: codePictureHeader
32
*
33
*  Input:    pic = pointer to picture information structure
34
*
35
*  Return:   none
36
*
37
*  Purpose:  To encode H.263 picture header
38
*
39
**---------------------------------------------------------------------------
40
*/
41
void codePictureHeader( const PictureType * const pic, BitStreamType * const stream ){
42
 
43
  /* Picture start code */
44
  bitstreamPut(22,0x20,stream);
45
 
46
  /* Temporal reference */
47
  bitstreamPut(8,pic->TR,stream);
48
 
49
  /* PTYPE */
50
  /* bit 1 always 1 to avoid start code emulation:              1 */
51
  /* bit 2 always zero for distinction with H.261:              0 */
52
  /* bit 3 no support for split-screen in this software:        0 */
53
  /* bit 4 document_camera indicator:                           0 */
54
  /* bit 5 freeze_picture_release:                              0 */
55
  /* bit 6-8 QCIF source format:                            0b010 */
56
  /* bit 9 Picture coding type is INTRA always:                 0 */
57
  /* bit 10 no support for Unrestricted Motion Vector mode:     0 */
58
  /* bit 11 no support for Syntax-based Arithmetic Coding mode: 0 */
59
  /* bit 12 no support for Advanced Prediction mode:            0 */
60
  /* bit 13 no support for PB-mode:                             0 */
61
  bitstreamPut(13,0x1040,stream);
62
 
63
  /* PQUANT */
64
  bitstreamPut(5,pic->QUANT,stream);
65
 
66
  /* Continuous Presence Multipoint (CPM) */
67
  bitstreamPut(1,0,stream);  /* CPM is not supported  */
68
 
69
  /* Picture Sub Bitstream Indicator (PSBI) */
70
  /* not needed */
71
 
72
  /* TRB and DBQUANT: extra information for PB-frames */
73
  /* PB not needed */
74
 
75
  /* PEI (extra information) */
76
  /* "Encoders shall not insert PSPARE until specified by the ITU" */
77
  bitstreamPut(1,0,stream);
78
 
79
  /* PSPARE */
80
  /* not supported */
81
 
82
}
83
 
84
 
85
/*---------------------------------------------------------------------------
86
*
87
*  Function: codeLastRunLevel
88
*
89
*  Input:    last = indication, if this is the last coefficient to be coded
90
*            run = number of zeros after previously coded coefficient
91
*            level = absolute value of coefficient to be coded
92
*
93
*  Return:   none
94
*
95
*  Purpose:  Uses H.263 escape coding or standard given codes to encode
96
*            given data
97
*
98
**---------------------------------------------------------------------------
99
*/
100
static void codeLastRunLevel( const sint32 last,
101
                              const sint32 run,
102
                              const sint32 level,
103
                              BitStreamType * const stream ) {
104
 
105
  sint32 sign = 0;
106
 
107
  if( level < 0 ){
108
    sign = 1;
109
  }
110
  /* Is escape coding necessary?   */
111
  if( vlcCodeCoefficient(last,run,abs(level),sign,stream) == V_FALSE ) {
112
    /* 7-bit H.263 escape code */
113
    bitstreamPut(7,3,stream);
114
    /* 1-bit last coefficient indication  */
115
    bitstreamPut(1,last,stream);
116
    /* 6-bit run count */
117
    bitstreamPut(6,run,stream);
118
    /* 8-bit level */
119
    bitstreamPut(8,(level & 0xff),stream);
120
    /* printf("Last %4i Run %4i Level %4i\n",last,run,level); */
121
  }
122
}
123
 
124
 
125
/*---------------------------------------------------------------------------
126
*
127
*  Function: codeIntraBlocks
128
*
129
*  Input:    MB = pointer to macroblock data
130
*            stream = encoding parameters and buffers
131
*
132
*  Return:   none
133
*
134
*  Purpose:  Encodes all six 8 x 8 blocks belonging to the macroblock
135
*            with last, run, level coding. Intra macroblocks will
136
*            always include the DC coefficient.
137
*
138
**---------------------------------------------------------------------------
139
*/
140
void codeIntraBlocks(const MBType * const MB,
141
                    BitStreamType * const stream){
142
 
143
  sint16 value;
144
  sint32 index;
145
  sint32 run;
146
  sint32 block;
147
  sint32 previous_run;
148
  sint32 level;
149
  sint32 mask = 32;
150
  const sint16 * coeffs;
151
 
152
  /* Loop through all 8 x 8 blocks  */
153
  for( block = 0; block < 6; block++ ) {
154
 
155
    coeffs = MB->data + block*64;
156
 
157
    /* DC coefficient */
158
    if( coeffs[0] == 128 )
159
      bitstreamPut(8,255,stream);
160
    else
161
      bitstreamPut(8,coeffs[0],stream);
162
 
163
    /* AC coefficients */
164
    /* Last, run, level scanning part  */
165
    if( ( MB->cbp & mask ) != 0 ) {
166
 
167
      /* Init the scanning system  */
168
      index = 1;
169
      run = 0;
170
      previous_run = 0;
171
      level = 0;
172
 
173
      /* Pre-scan because of LAST must be known  */
174
      while( index < 64 ) {
175
 
176
        /* Notice how zigzag scan is taken in the account */
177
        value = coeffs[ zigzagScan[index++] ];
178
        if( value != 0 ) {
179
          level = value;
180
          previous_run = run;
181
          run = 0;
182
          break;
183
        }
184
        run++;
185
      }
186
      /* Scan possible remaining coefficients  */
187
      while( index < 64 ) {
188
        value = coeffs[ zigzagScan[index++] ];
189
        if( value != 0 ) {
190
          codeLastRunLevel(0,previous_run,level,stream);
191
          level = value;
192
          previous_run = run;
193
          run = 0;
194
        }
195
        else {
196
          run++;
197
        }
198
      }
199
      /* Put the last coefficient into the bitstream  */
200
      codeLastRunLevel(1,previous_run,level,stream);
201
    }
202
 
203
     /* Move to next block */
204
      mask = mask >> 1;
205
  }
206
}
207
 
208
 
209
/*---------------------------------------------------------------------------
210
*
211
*  Function: codeIntraMB
212
*
213
*  Input:    MB = pointer to macroblock data
214
*            stream = encoding parameters and buffers
215
*
216
*  Return:   none
217
*
218
*  Purpose:  To manage VLC encoding of Intra macroblocks (headers + blocks)
219
*
220
**---------------------------------------------------------------------------
221
*/
222
void codeIntraMB(const MBType * const MB, BitStreamType * const stream){
223
 
224
  sint32 bitCount;
225
  sint32 bitPattern;
226
  sint32 chromaCbp;
227
  sint32 lumaCbp;
228
 
229
  /* COD, MODB, CBPB, DQUANT, MVD, MVD(2-4) and MVDB not needed */
230
 
231
  /* CBPCM */
232
  chromaCbp = MB->cbp & 3;
233
  bitCount = mcbpcIntraTable[0][chromaCbp].bitCount;
234
  bitPattern = mcbpcIntraTable[0][chromaCbp].bitPattern;
235
  bitstreamPut(bitCount,bitPattern,stream);
236
 
237
  /* CBPY */
238
  lumaCbp = MB->cbp >> 2;
239
  bitCount = cbpyTable[lumaCbp].bitCount;
240
  bitPattern = cbpyTable[lumaCbp].bitPattern;
241
  bitstreamPut(bitCount,bitPattern,stream);
242
 
243
  /* Block layer encoding */
244
  codeIntraBlocks(MB,stream);
245
 
246
}
247
 

powered by: WebSVN 2.1.0

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