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