fbui | Framebuffer-based graphical environment |
| download: https://git.y1.nz/archives/fbui.tar.gz | |
| README | Files | Log | Refs |
libfbui/MPEG/verify.c
1 /* 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 /* #define DEBUG */
19 #ifdef DEBUG
20 #define PC
21 #endif
22
23 #ifdef PC
24 #include <conio.h> /* needed for getch() */
25 #endif /* PC */
26
27 /*
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
31 start code stuffing).
32 */
33
34
35 static void Check_VBV_Delay _ANSI_ARGS_((int Bitstream_Framenum, int Sequence_Framenum));
36
37
38 void Check_Headers(Bitstream_Framenum, Sequence_Framenum)
39 int Bitstream_Framenum;
40 int Sequence_Framenum;
41 {
42
43
44 if((!low_delay)&&(vbv_delay!=0)&&(vbv_delay!=0xFFFF))
45 Check_VBV_Delay(Bitstream_Framenum, Sequence_Framenum);
46
47 /* clear out the header tracking variables so we have an accurate
48 count next time */
49 Clear_Verify_Headers();
50 }
51
52
53
54 /*
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)
59 *
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!
63 *
64 * Also: no special provisions at the beginning or end of a sequence
65 */
66
67 static void Check_VBV_Delay(Bitstream_Framenum, Sequence_Framenum)
68 int Bitstream_Framenum;
69 int Sequence_Framenum;
70 {
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) */
76
77 int d;
78 int internal_vbv_delay;
79
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;
85
86 static double previous_Bn;
87 static double E; /* maximum quantization error or mismatch */
88
89
90
91 if((Sequence_Framenum==0)&&(!Second_Field))
92 { /* first coded picture of sequence */
93
94 R = bit_rate;
95
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 */
99
100 Bn = (R * (double) vbv_delay) / 90000.0;
101 B = 16 * 1024 * vbv_buffer_size;
102
103
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
106 section 6.3.3 */
107
108 E = (400.0/frame_rate) + 400;
109
110 #ifdef DEBUG
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);
113 #endif
114
115 }
116 else /* not the first coded picture of sequence */
117 {
118
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
121 *
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)
126 *
127 * 2. B pictures are output ("displayed") at the time when they are decoded
128 *
129 */
130
131 if(progressive_sequence) /* Annex C.9 (progressive_sequence==1, low_delay==0) */
132 {
133
134 T = 1/frame_rate; /* inverse of the frame rate (frame period) */
135
136 if(picture_coding_type==B_TYPE)
137 {
138 if(repeat_first_field==1)
139 {
140 if(top_field_first==1)
141 I = T*3; /* three frame periods */
142 else
143 I = T*2; /* two frame periods */
144 }
145 else
146 I = T; /* one frame period */
147 }
148 else /* P or I frame */
149 {
150 if(previous_IorP_repeat_first_field==1)
151 {
152 if(previous_IorP_top_field_first==1)
153 I = 3*T;
154 else
155 I = 2*T;
156 }
157 else
158 I = T;
159 }
160 }
161 else /* Annex C.11 (progressive_sequence==0, low_delay==0) */
162 {
163
164 T = 1/(2*frame_rate); /* inverse of two times the frame rate (field period) */
165
166 if(picture_coding_type==B_TYPE)
167 {
168 if(picture_structure==FRAME_PICTURE)
169 {
170 if(repeat_first_field==0)
171 I = 2*T; /* two field periods */
172 else
173 I = 3*T; /* three field periods */
174 }
175 else /* B field */
176 {
177 I = T; /* one field period */
178 }
179 }
180 else /* I or P picture */
181 {
182 if(picture_structure==FRAME_PICTURE)
183 {
184 if(previous_IorP_repeat_first_field==0)
185 I = 2*T;
186 else
187 I = 3*T;
188 }
189 else
190 {
191 if(Second_Field==0) /* first field of current frame */
192 I = T;
193 else /* second field of current frame */
194 {
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
197 frame */
198
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 */
205 }
206 }
207 }
208 }
209
210 /* derive coded size of previous picture */
211 d = ld->Bitcnt - previous_bitstream_position;
212
213 /* Rate = Distance/Time */
214
215 /* piecewise constant rate (variable rate stream) calculation
216 * R = ((double) d /((previous_vbv_delay - vbv_delay)/90000 + I));
217 */
218
219 R = bit_rate;
220
221 /* compute buffer fullness just before removing picture n
222 *
223 * Bn = previous_Bn + (I*R) - d; (recursive formula)
224 *
225 * where:
226 *
227 * n is the current picture
228 *
229 * Bn is the buffer fullness for the current picture
230 *
231 * previous_Bn is the buffer fullness of the previous picture
232 *
233 * (I*R ) is the bits accumulated during the current picture
234 * period
235 *
236 * d is the number of bits removed during the decoding of the
237 * previous picture
238 */
239
240 Bn = previous_Bn + (I*R) - d;
241
242 /* compute internally derived vbv_delay (rouding up with ceil()) */
243 internal_vbv_delay = (int) ceil((90000 * Bn / bit_rate));
244
245 #ifdef DEBUG
246 printf("\nvbv_delay: internal=%d, bitstream=%d\n", internal_vbv_delay, vbv_delay);
247
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);
251
252 /* report error */
253 if(internal_vbv_delay != vbv_delay)
254 {
255 printf("WARNING: internal_vbv_delay(%d) != vbv_delay(%d)\n",
256 internal_vbv_delay, vbv_delay);
257 }
258 #endif
259
260 } /* not the first coded picture of sequence */
261
262
263 #ifdef PC
264 getch();
265 #endif /* PC */
266
267 /* update generic tracking variables */
268 previous_bitstream_position = ld->Bitcnt ;
269 previous_vbv_delay = vbv_delay;
270 previous_Bn = Bn;
271
272 /* reference picture: reordered/delayed output picture */
273 if(picture_coding_type!=B_TYPE)
274 {
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;
278 }
279
280 }
281
282
283
284 /* variables to keep track of the occurance of redundant headers between pictures */
285 void Clear_Verify_Headers()
286 {
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;
300 }
301
302 #endif /* VERIFY */
303
This webpage is intended to be an accessible preview of this repository. To get a fuller picture, clone it and use the git CLI.