3 * Bitstream verification routines
13 #include <math.h> /* needed for ceil() */
24 #include <conio.h> /* needed for getch() */
28 Check picture headers: due to the VBV definition of picture data,
29 this routine must be called immediately before any picture data
30 is parsed. (before the first slice start code, including any slice
35 static void Check_VBV_Delay _ANSI_ARGS_((int Bitstream_Framenum, int Sequence_Framenum));
38 void Check_Headers(Bitstream_Framenum, Sequence_Framenum)
39 int Bitstream_Framenum;
40 int Sequence_Framenum;
44 if((!low_delay)&&(vbv_delay!=0)&&(vbv_delay!=0xFFFF))
45 Check_VBV_Delay(Bitstream_Framenum, Sequence_Framenum);
47 /* clear out the header tracking variables so we have an accurate
49 Clear_Verify_Headers();
55 * Verify vbv_delay value in picture header
56 * (low_delay==1 checks not implemented. this does not exhaustively test all
57 * possibilities suggested in ISO/IEC 13818-2 Annex C. It only checks
58 * for constant rate streams)
60 * Q:how do we tell a variable rate stream from a constant rate stream anyway?
61 * it's not as simple as vbv_delay==0xFFFF, since we need meaningful
62 * vbv_delay values to calculate the piecewise rate in the first place!
64 * Also: no special provisions at the beginning or end of a sequence
67 static void Check_VBV_Delay(Bitstream_Framenum, Sequence_Framenum)
68 int Bitstream_Framenum;
69 int Sequence_Framenum;
71 double B; /* buffer size */
72 double Bn; /* buffer fullness for picture n */
73 double R; /* bitrate */
74 double I; /* time interval (t[n+1] - t[n]) */
75 double T; /* inverse of the frame rate (frame period) */
78 int internal_vbv_delay;
80 static int previous_IorP_picture_structure;
81 static int previous_IorP_repeat_first_field;
82 static int previous_IorP_top_field_first;
83 static int previous_vbv_delay;
84 static int previous_bitstream_position;
86 static double previous_Bn;
87 static double E; /* maximum quantization error or mismatch */
91 if((Sequence_Framenum==0)&&(!Second_Field))
92 { /* first coded picture of sequence */
96 /* the initial buffer occupancy is taken on faith
97 that is, we believe what is transmitted in the first coded picture header
98 to be the true/actual buffer occupancy */
100 Bn = (R * (double) vbv_delay) / 90000.0;
101 B = 16 * 1024 * vbv_buffer_size;
104 /* maximum quantization error in bitrate (bit_rate_value is quantized/
105 rounded-up to units of 400 bits/sec as per ISO/IEC 13818-2
108 E = (400.0/frame_rate) + 400;
111 printf("vbv_buffer_size (B) = %.0f, Bn=%f, E=%f, \nbitrate=%f, vbv_delay=%d frame_rate=%f\n",
112 B, Bn, E, bit_rate, vbv_delay, frame_rate);
116 else /* not the first coded picture of sequence */
119 /* derive the interval (I). The interval tells us how many constant rate bits
120 * will have been downloaded to the buffer during the current picture period
122 * interval assumes that:
123 * 1. whilst we are decoding the current I or P picture, we are displaying
124 * the previous I or P picture which was stored in the reorder
125 * buffer (pointed to by forward_reference_frame in this implementation)
127 * 2. B pictures are output ("displayed") at the time when they are decoded
131 if(progressive_sequence) /* Annex C.9 (progressive_sequence==1, low_delay==0) */
134 T = 1/frame_rate; /* inverse of the frame rate (frame period) */
136 if(picture_coding_type==B_TYPE)
138 if(repeat_first_field==1)
140 if(top_field_first==1)
141 I = T*3; /* three frame periods */
143 I = T*2; /* two frame periods */
146 I = T; /* one frame period */
148 else /* P or I frame */
150 if(previous_IorP_repeat_first_field==1)
152 if(previous_IorP_top_field_first==1)
161 else /* Annex C.11 (progressive_sequence==0, low_delay==0) */
164 T = 1/(2*frame_rate); /* inverse of two times the frame rate (field period) */
166 if(picture_coding_type==B_TYPE)
168 if(picture_structure==FRAME_PICTURE)
170 if(repeat_first_field==0)
171 I = 2*T; /* two field periods */
173 I = 3*T; /* three field periods */
177 I = T; /* one field period */
180 else /* I or P picture */
182 if(picture_structure==FRAME_PICTURE)
184 if(previous_IorP_repeat_first_field==0)
191 if(Second_Field==0) /* first field of current frame */
193 else /* second field of current frame */
195 /* formula: previous I or P display period (2*T or 3*T) minus the
196 very recent decode period (T) of the first field of the current
199 if(previous_IorP_picture_structure!=FRAME_PICTURE
200 || previous_IorP_repeat_first_field==0)
201 I = 2*T - T; /* a net of one field period */
202 else if(previous_IorP_picture_structure==FRAME_PICTURE
203 && previous_IorP_repeat_first_field==1)
204 I = 3*T - T; /* a net of two field periods */
210 /* derive coded size of previous picture */
211 d = ld->Bitcnt - previous_bitstream_position;
213 /* Rate = Distance/Time */
215 /* piecewise constant rate (variable rate stream) calculation
216 * R = ((double) d /((previous_vbv_delay - vbv_delay)/90000 + I));
221 /* compute buffer fullness just before removing picture n
223 * Bn = previous_Bn + (I*R) - d; (recursive formula)
227 * n is the current picture
229 * Bn is the buffer fullness for the current picture
231 * previous_Bn is the buffer fullness of the previous picture
233 * (I*R ) is the bits accumulated during the current picture
236 * d is the number of bits removed during the decoding of the
240 Bn = previous_Bn + (I*R) - d;
242 /* compute internally derived vbv_delay (rouding up with ceil()) */
243 internal_vbv_delay = (int) ceil((90000 * Bn / bit_rate));
246 printf("\nvbv_delay: internal=%d, bitstream=%d\n", internal_vbv_delay, vbv_delay);
248 printf("Bn=%f, prevBn=%f, I=%f, R=%f, d=%d\n", Bn, previous_Bn, I, R, d);
249 printf("frame(%d), pictstruct(%d), picttype(%d)\n", Sequence_Framenum,
250 picture_structure, picture_coding_type);
253 if(internal_vbv_delay != vbv_delay)
255 printf("WARNING: internal_vbv_delay(%d) != vbv_delay(%d)\n",
256 internal_vbv_delay, vbv_delay);
260 } /* not the first coded picture of sequence */
267 /* update generic tracking variables */
268 previous_bitstream_position = ld->Bitcnt ;
269 previous_vbv_delay = vbv_delay;
272 /* reference picture: reordered/delayed output picture */
273 if(picture_coding_type!=B_TYPE)
275 previous_IorP_repeat_first_field = repeat_first_field;
276 previous_IorP_top_field_first = top_field_first;
277 previous_IorP_picture_structure = picture_structure;
284 /* variables to keep track of the occurance of redundant headers between pictures */
285 void Clear_Verify_Headers()
287 verify_sequence_header = 0;
288 verify_group_of_pictures_header = 0;
289 verify_picture_header = 0;
290 verify_slice_header = 0;
291 verify_sequence_extension = 0;
292 verify_sequence_display_extension = 0;
293 verify_quant_matrix_extension = 0;
294 verify_sequence_scalable_extension = 0;
295 verify_picture_display_extension = 0;
296 verify_picture_coding_extension = 0;
297 verify_picture_spatial_scalable_extension = 0;
298 verify_picture_temporal_scalable_extension = 0;
299 verify_copyright_extension = 0;