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

Subversion Repositories mpeg2fpga

[/] [mpeg2fpga/] [trunk/] [tools/] [mpeg2dec/] [verify.c] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 kdv
/* verify.c
2
 *
3
 * Bitstream verification routines
4
 *
5
 *
6
 */
7
#ifdef VERIFY 
8
 
9
#include <stdio.h>
10
#include <stdlib.h>
11
#include <ctype.h>
12
#include <fcntl.h>
13
#include <math.h>     /* needed for ceil() */
14
 
15
#include "config.h"
16
#include "global.h"
17
 
18
#ifdef PC
19
#include <conio.h>    /* needed for getch() */
20
#endif /* PC */
21
 
22
/*
23
   Check picture headers:  due to the VBV definition of picture data,
24
   this routine must be called immediately before any picture data
25
   is parsed. (before the first slice start code, including any slice
26
   start code stuffing).
27
*/
28
 
29
 
30
static void Check_VBV_Delay _ANSI_ARGS_((int Bitstream_Framenum, int Sequence_Framenum));
31
 
32
 
33
void Check_Headers(Bitstream_Framenum, Sequence_Framenum)
34
int Bitstream_Framenum;
35
int Sequence_Framenum;
36
{
37
 
38
 
39
  if((!low_delay)&&(vbv_delay!=0)&&(vbv_delay!=0xFFFF))
40
    Check_VBV_Delay(Bitstream_Framenum, Sequence_Framenum);
41
 
42
  /* clear out the header tracking variables so we have an accurate
43
     count next time */
44
  Clear_Verify_Headers();
45
}
46
 
47
 
48
 
49
/*
50
 * Verify vbv_delay value in picture header
51
 * (low_delay==1 checks not implemented. this does not exhaustively test all
52
 *  possibilities suggested in ISO/IEC 13818-2 Annex C.  It only checks
53
 *  for constant rate streams)
54
 *
55
 * Q:how do we tell a variable rate stream from a constant rate stream anyway?
56
 *   it's not as simple as vbv_delay==0xFFFF, since we need meaningful
57
 *   vbv_delay values to calculate the piecewise rate in the first place!
58
 *
59
 * Also: no special provisions at the beginning or end of a sequence
60
 */
61
 
62
static void Check_VBV_Delay(Bitstream_Framenum, Sequence_Framenum)
63
int Bitstream_Framenum;
64
int Sequence_Framenum;
65
{
66
  double B;   /* buffer size                   */
67
  double Bn;  /* buffer fullness for picture n */
68
  double R;   /* bitrate                       */
69
  double I;   /* time interval (t[n+1] - t[n]) */
70
  double T;   /* inverse of the frame rate (frame period) */
71
 
72
  int d;
73
  int internal_vbv_delay;
74
 
75
  static int previous_IorP_picture_structure;
76
  static int previous_IorP_repeat_first_field;
77
  static int previous_IorP_top_field_first;
78
  static int previous_vbv_delay;
79
  static int previous_bitstream_position;
80
 
81
  static double previous_Bn;
82
  static double E;      /* maximum quantization error or mismatch */
83
 
84
 
85
 
86
  if((Sequence_Framenum==0)&&(!Second_Field))
87
  {  /* first coded picture of sequence */
88
 
89
    R = bit_rate;
90
 
91
    /* the initial buffer occupancy is taken on faith
92
       that is, we believe what is transmitted in the first coded picture header
93
       to be the true/actual buffer occupancy */
94
 
95
    Bn = (R * (double) vbv_delay) / 90000.0;
96
    B = 16 * 1024 * vbv_buffer_size;
97
 
98
 
99
    /* maximum quantization error in bitrate (bit_rate_value is quantized/
100
       rounded-up to units of 400 bits/sec as per ISO/IEC 13818-2
101
       section 6.3.3 */
102
 
103
    E = (400.0/frame_rate) + 400;
104
 
105
#ifdef DEBUG
106
    printf("vbv_buffer_size (B) = %.0f, Bn=%f, E=%f, \nbitrate=%f, vbv_delay=%d frame_rate=%f\n",
107
      B, Bn, E, bit_rate, vbv_delay, frame_rate);
108
#endif
109
 
110
  }
111
  else /* not the first coded picture of sequence */
112
  {
113
 
114
    /* derive the interval (I).  The interval tells us how many constant rate bits
115
     * will have been downloaded to the buffer during the current picture period
116
     *
117
     * interval assumes that:
118
     *  1. whilst we are decoding the current I or P picture, we are displaying
119
     *     the previous I or P picture which was stored in the reorder
120
     *     buffer (pointed to by forward_reference_frame in this implementation)
121
     *
122
     *  2. B pictures are output ("displayed") at the time when they are decoded
123
     *
124
     */
125
 
126
    if(progressive_sequence) /* Annex C.9 (progressive_sequence==1, low_delay==0) */
127
    {
128
 
129
      T = 1/frame_rate; /* inverse of the frame rate (frame period) */
130
 
131
      if(picture_coding_type==B_TYPE)
132
      {
133
        if(repeat_first_field==1)
134
        {
135
          if(top_field_first==1)
136
            I = T*3;  /* three frame periods */
137
          else
138
            I = T*2;  /* two frame periods */
139
        }
140
        else
141
          I = T;      /* one frame period */
142
      }
143
      else /* P or I frame */
144
      {
145
        if(previous_IorP_repeat_first_field==1)
146
        {
147
          if(previous_IorP_top_field_first==1)
148
            I = 3*T;
149
          else
150
            I = 2*T;
151
        }
152
        else
153
          I = T;
154
      }
155
    }
156
    else /* Annex C.11 (progressive_sequence==0, low_delay==0) */
157
    {
158
 
159
      T = 1/(2*frame_rate); /* inverse of two times the frame rate (field period) */
160
 
161
      if(picture_coding_type==B_TYPE)
162
      {
163
        if(picture_structure==FRAME_PICTURE)
164
        {
165
          if(repeat_first_field==0)
166
            I = 2*T;  /* two field periods */
167
          else
168
            I = 3*T;  /* three field periods */
169
        }
170
        else /* B field */
171
        {
172
          I = T;      /* one field period */
173
        }
174
      }
175
      else /* I or P picture */
176
      {
177
        if(picture_structure==FRAME_PICTURE)
178
        {
179
          if(previous_IorP_repeat_first_field==0)
180
            I = 2*T;
181
          else
182
            I = 3*T;
183
        }
184
        else
185
        {
186
          if(Second_Field==0)  /* first field of current frame */
187
            I = T;
188
          else /* second field of current frame */
189
          {
190
            /* formula: previous I or P display period (2*T or 3*T) minus the
191
               very recent decode period (T) of the first field of the current
192
               frame */
193
 
194
            if(previous_IorP_picture_structure!=FRAME_PICTURE
195
              || previous_IorP_repeat_first_field==0)
196
              I = 2*T - T;  /* a net of one field period */
197
            else if(previous_IorP_picture_structure==FRAME_PICTURE
198
              && previous_IorP_repeat_first_field==1)
199
              I = 3*T - T;  /* a net of two field periods */
200
          }
201
        }
202
      }
203
    }
204
 
205
    /* derive coded size of previous picture */
206
    d  = ld->Bitcnt - previous_bitstream_position;
207
 
208
    /* Rate = Distance/Time */
209
 
210
    /* piecewise constant rate (variable rate stream) calculation
211
     * R =  ((double) d /((previous_vbv_delay - vbv_delay)/90000 + I));
212
     */
213
 
214
    R = bit_rate;
215
 
216
    /* compute buffer fullness just before removing picture n
217
     *
218
     * Bn = previous_Bn + (I*R) - d;     (recursive formula)
219
     *
220
     *   where:
221
     *
222
     *    n           is the current picture
223
     *
224
     *    Bn          is the buffer fullness for the current picture
225
     *
226
     *    previous_Bn is the buffer fullness of the previous picture
227
     *
228
     *    (I*R )      is the bits accumulated during the current picture
229
     *                period
230
     *
231
     *    d           is the number of bits removed during the decoding of the
232
     *                previous picture
233
     */
234
 
235
    Bn = previous_Bn + (I*R) - d;
236
 
237
    /* compute internally derived vbv_delay (rouding up with ceil()) */
238
    internal_vbv_delay = (int) ceil((90000 * Bn / bit_rate));
239
 
240
#ifdef DEBUG
241
    printf("\nvbv_delay: internal=%d, bitstream=%d\n", internal_vbv_delay, vbv_delay);
242
 
243
    printf("Bn=%f, prevBn=%f, I=%f, R=%f, d=%d\n", Bn, previous_Bn, I, R, d);
244
    printf("frame(%d), pictstruct(%d), picttype(%d)\n", Sequence_Framenum,
245
      picture_structure, picture_coding_type);
246
 
247
    /* report error */
248
    if(internal_vbv_delay != vbv_delay)
249
    {
250
      printf("WARNING: internal_vbv_delay(%d) != vbv_delay(%d)\n",
251
        internal_vbv_delay, vbv_delay);
252
    }
253
#endif
254
 
255
  } /* not the first coded picture of sequence */
256
 
257
 
258
#ifdef PC
259
  getch();
260
#endif /* PC */
261
 
262
  /* update generic tracking variables */
263
  previous_bitstream_position = ld->Bitcnt ;
264
  previous_vbv_delay          = vbv_delay;
265
  previous_Bn                 = Bn;
266
 
267
  /* reference picture: reordered/delayed output picture */
268
  if(picture_coding_type!=B_TYPE)
269
  {
270
    previous_IorP_repeat_first_field = repeat_first_field;
271
    previous_IorP_top_field_first    = top_field_first;
272
    previous_IorP_picture_structure  = picture_structure;
273
  }
274
 
275
}
276
 
277
 
278
 
279
/* variables to keep track of the occurance of redundant headers between pictures */
280
void Clear_Verify_Headers()
281
{
282
  verify_sequence_header = 0;
283
  verify_group_of_pictures_header = 0;
284
  verify_picture_header = 0;
285
  verify_slice_header = 0;
286
  verify_sequence_extension = 0;
287
  verify_sequence_display_extension = 0;
288
  verify_quant_matrix_extension = 0;
289
  verify_sequence_scalable_extension = 0;
290
  verify_picture_display_extension = 0;
291
  verify_picture_coding_extension = 0;
292
  verify_picture_spatial_scalable_extension = 0;
293
  verify_picture_temporal_scalable_extension = 0;
294
  verify_copyright_extension = 0;
295
}
296
 
297
#endif /* VERIFY */
298
 

powered by: WebSVN 2.1.0

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