1 |
2 |
kdv |
/* getpic.c, picture decoding */
|
2 |
|
|
|
3 |
|
|
/* Copyright (C) 1996, MPEG Software Simulation Group. All Rights Reserved. */
|
4 |
|
|
|
5 |
|
|
/*
|
6 |
|
|
* Disclaimer of Warranty
|
7 |
|
|
*
|
8 |
|
|
* These software programs are available to the user without any license fee or
|
9 |
|
|
* royalty on an "as is" basis. The MPEG Software Simulation Group disclaims
|
10 |
|
|
* any and all warranties, whether express, implied, or statuary, including any
|
11 |
|
|
* implied warranties or merchantability or of fitness for a particular
|
12 |
|
|
* purpose. In no event shall the copyright-holder be liable for any
|
13 |
|
|
* incidental, punitive, or consequential damages of any kind whatsoever
|
14 |
|
|
* arising from the use of these programs.
|
15 |
|
|
*
|
16 |
|
|
* This disclaimer of warranty extends to the user of these programs and user's
|
17 |
|
|
* customers, employees, agents, transferees, successors, and assigns.
|
18 |
|
|
*
|
19 |
|
|
* The MPEG Software Simulation Group does not represent or warrant that the
|
20 |
|
|
* programs furnished hereunder are free of infringement of any third-party
|
21 |
|
|
* patents.
|
22 |
|
|
*
|
23 |
|
|
* Commercial implementations of MPEG-1 and MPEG-2 video, including shareware,
|
24 |
|
|
* are subject to royalty fees to patent holders. Many of these patents are
|
25 |
|
|
* general enough such that they are unavoidable regardless of implementation
|
26 |
|
|
* design.
|
27 |
|
|
*
|
28 |
|
|
*/
|
29 |
|
|
|
30 |
|
|
#include
|
31 |
|
|
|
32 |
|
|
#include "config.h"
|
33 |
|
|
#include "global.h"
|
34 |
|
|
|
35 |
|
|
/* private prototypes*/
|
36 |
|
|
static void picture_data _ANSI_ARGS_((int framenum));
|
37 |
|
|
static void macroblock_modes _ANSI_ARGS_((int *pmacroblock_type, int *pstwtype,
|
38 |
|
|
int *pstwclass, int *pmotion_type, int *pmotion_vector_count, int *pmv_format, int *pdmv,
|
39 |
|
|
int *pmvscale, int *pdct_type));
|
40 |
|
|
static void Clear_Block _ANSI_ARGS_((int comp));
|
41 |
|
|
static void Sum_Block _ANSI_ARGS_((int comp));
|
42 |
|
|
static void Saturate _ANSI_ARGS_((short *bp));
|
43 |
|
|
static void Add_Block _ANSI_ARGS_((int comp, int bx, int by,
|
44 |
|
|
int dct_type, int addflag));
|
45 |
|
|
static void Update_Picture_Buffers _ANSI_ARGS_((void));
|
46 |
|
|
static void frame_reorder _ANSI_ARGS_((int bitstream_framenum,
|
47 |
|
|
int sequence_framenum));
|
48 |
|
|
static void Decode_SNR_Macroblock _ANSI_ARGS_((int *SNRMBA, int *SNRMBAinc,
|
49 |
|
|
int MBA, int MBAmax, int *dct_type));
|
50 |
|
|
|
51 |
|
|
static void motion_compensation _ANSI_ARGS_((int MBA, int macroblock_type,
|
52 |
|
|
int motion_type, int PMV[2][2][2], int motion_vertical_field_select[2][2],
|
53 |
|
|
int dmvector[2], int stwtype, int dct_type));
|
54 |
|
|
|
55 |
|
|
static void skipped_macroblock _ANSI_ARGS_((int dc_dct_pred[3],
|
56 |
|
|
int PMV[2][2][2], int *motion_type, int motion_vertical_field_select[2][2],
|
57 |
|
|
int *stwtype, int *macroblock_type));
|
58 |
|
|
|
59 |
|
|
static int slice _ANSI_ARGS_((int framenum, int MBAmax));
|
60 |
|
|
|
61 |
|
|
static int start_of_slice _ANSI_ARGS_ ((int MBAmax, int *MBA,
|
62 |
|
|
int *MBAinc, int dc_dct_pred[3], int PMV[2][2][2]));
|
63 |
|
|
|
64 |
|
|
static int decode_macroblock _ANSI_ARGS_((int *macroblock_type,
|
65 |
|
|
int *stwtype, int *stwclass, int *motion_type, int *dct_type,
|
66 |
|
|
int PMV[2][2][2], int dc_dct_pred[3],
|
67 |
|
|
int motion_vertical_field_select[2][2], int dmvector[2]));
|
68 |
|
|
|
69 |
|
|
|
70 |
|
|
/* decode one frame or field picture */
|
71 |
|
|
void Decode_Picture(bitstream_framenum, sequence_framenum)
|
72 |
|
|
int bitstream_framenum, sequence_framenum;
|
73 |
|
|
{
|
74 |
|
|
|
75 |
|
|
if (picture_structure==FRAME_PICTURE && Second_Field)
|
76 |
|
|
{
|
77 |
|
|
/* recover from illegal number of field pictures */
|
78 |
|
|
printf("odd number of field pictures\n");
|
79 |
|
|
Second_Field = 0;
|
80 |
|
|
}
|
81 |
|
|
|
82 |
|
|
/* IMPLEMENTATION: update picture buffer pointers */
|
83 |
|
|
Update_Picture_Buffers();
|
84 |
|
|
|
85 |
|
|
#ifdef VERIFY
|
86 |
|
|
Check_Headers(bitstream_framenum, sequence_framenum);
|
87 |
|
|
#endif /* VERIFY */
|
88 |
|
|
|
89 |
|
|
/* ISO/IEC 13818-4 section 2.4.5.4 "frame buffer intercept method" */
|
90 |
|
|
/* (section number based on November 1995 (Dallas) draft of the
|
91 |
|
|
conformance document) */
|
92 |
|
|
if(Ersatz_Flag)
|
93 |
|
|
Substitute_Frame_Buffer(bitstream_framenum, sequence_framenum);
|
94 |
|
|
|
95 |
|
|
/* form spatial scalable picture */
|
96 |
|
|
|
97 |
|
|
/* form spatial scalable picture */
|
98 |
|
|
/* ISO/IEC 13818-2 section 7.7: Spatial scalability */
|
99 |
|
|
if (base.pict_scal && !Second_Field)
|
100 |
|
|
{
|
101 |
|
|
Spatial_Prediction();
|
102 |
|
|
}
|
103 |
|
|
|
104 |
|
|
/* decode picture data ISO/IEC 13818-2 section 6.2.3.7 */
|
105 |
|
|
picture_data(bitstream_framenum);
|
106 |
|
|
|
107 |
|
|
/* write or display current or previously decoded reference frame */
|
108 |
|
|
/* ISO/IEC 13818-2 section 6.1.1.11: Frame reordering */
|
109 |
|
|
frame_reorder(bitstream_framenum, sequence_framenum);
|
110 |
|
|
|
111 |
|
|
if (picture_structure!=FRAME_PICTURE)
|
112 |
|
|
Second_Field = !Second_Field;
|
113 |
|
|
}
|
114 |
|
|
|
115 |
|
|
|
116 |
|
|
/* decode all macroblocks of the current picture */
|
117 |
|
|
/* stages described in ISO/IEC 13818-2 section 7 */
|
118 |
|
|
static void picture_data(framenum)
|
119 |
|
|
int framenum;
|
120 |
|
|
{
|
121 |
|
|
int MBAmax;
|
122 |
|
|
int ret;
|
123 |
|
|
|
124 |
|
|
/* number of macroblocks per picture */
|
125 |
|
|
MBAmax = mb_width*mb_height;
|
126 |
|
|
|
127 |
|
|
if (picture_structure!=FRAME_PICTURE)
|
128 |
|
|
MBAmax>>=1; /* field picture has half as mnay macroblocks as frame */
|
129 |
|
|
|
130 |
|
|
for(;;)
|
131 |
|
|
{
|
132 |
|
|
if((ret=slice(framenum, MBAmax))<0)
|
133 |
|
|
return;
|
134 |
|
|
}
|
135 |
|
|
|
136 |
|
|
}
|
137 |
|
|
|
138 |
|
|
|
139 |
|
|
|
140 |
|
|
/* decode all macroblocks of the current picture */
|
141 |
|
|
/* ISO/IEC 13818-2 section 6.3.16 */
|
142 |
|
|
static int slice(framenum, MBAmax)
|
143 |
|
|
int framenum, MBAmax;
|
144 |
|
|
{
|
145 |
|
|
int MBA;
|
146 |
|
|
int MBAinc, macroblock_type, motion_type, dct_type;
|
147 |
|
|
int dc_dct_pred[3];
|
148 |
|
|
int PMV[2][2][2], motion_vertical_field_select[2][2];
|
149 |
|
|
int dmvector[2];
|
150 |
|
|
int stwtype, stwclass;
|
151 |
|
|
int SNRMBA, SNRMBAinc;
|
152 |
|
|
int ret;
|
153 |
|
|
|
154 |
|
|
MBA = 0; /* macroblock address */
|
155 |
|
|
MBAinc = 0;
|
156 |
|
|
|
157 |
|
|
if((ret=start_of_slice(MBAmax, &MBA, &MBAinc, dc_dct_pred, PMV))!=1)
|
158 |
|
|
return(ret);
|
159 |
|
|
|
160 |
|
|
if (Two_Streams && enhan.scalable_mode==SC_SNR)
|
161 |
|
|
{
|
162 |
|
|
SNRMBA=0;
|
163 |
|
|
SNRMBAinc=0;
|
164 |
|
|
}
|
165 |
|
|
|
166 |
|
|
Fault_Flag=0;
|
167 |
|
|
|
168 |
|
|
for (;;)
|
169 |
|
|
{
|
170 |
|
|
|
171 |
|
|
/* this is how we properly exit out of picture */
|
172 |
|
|
if (MBA>=MBAmax)
|
173 |
|
|
return(-1); /* all macroblocks decoded */
|
174 |
|
|
|
175 |
|
|
#ifdef TRACE
|
176 |
|
|
if (Trace_Flag)
|
177 |
|
|
printf("frame %d, MB %d\n",framenum,MBA);
|
178 |
|
|
#endif /* TRACE */
|
179 |
|
|
|
180 |
|
|
#ifdef DISPLAY
|
181 |
|
|
if (!progressive_frame && picture_structure==FRAME_PICTURE
|
182 |
|
|
&& MBA==(MBAmax>>1) && framenum!=0 && Output_Type==T_X11
|
183 |
|
|
&& !Display_Progressive_Flag)
|
184 |
|
|
{
|
185 |
|
|
Display_Second_Field();
|
186 |
|
|
}
|
187 |
|
|
#endif
|
188 |
|
|
|
189 |
|
|
ld = &base;
|
190 |
|
|
|
191 |
|
|
if (MBAinc==0)
|
192 |
|
|
{
|
193 |
|
|
if (base.scalable_mode==SC_DP && base.priority_breakpoint==1)
|
194 |
|
|
ld = &enhan;
|
195 |
|
|
|
196 |
|
|
if (!Show_Bits(23) || Fault_Flag) /* next_start_code or fault */
|
197 |
|
|
{
|
198 |
|
|
resync: /* if Fault_Flag: resynchronize to next next_start_code */
|
199 |
|
|
Fault_Flag = 0;
|
200 |
|
|
return(0); /* trigger: go to next slice */
|
201 |
|
|
}
|
202 |
|
|
else /* neither next_start_code nor Fault_Flag */
|
203 |
|
|
{
|
204 |
|
|
if (base.scalable_mode==SC_DP && base.priority_breakpoint==1)
|
205 |
|
|
ld = &enhan;
|
206 |
|
|
|
207 |
|
|
/* decode macroblock address increment */
|
208 |
|
|
MBAinc = Get_macroblock_address_increment();
|
209 |
|
|
|
210 |
|
|
if (Fault_Flag) goto resync;
|
211 |
|
|
}
|
212 |
|
|
}
|
213 |
|
|
|
214 |
|
|
if (MBA>=MBAmax)
|
215 |
|
|
{
|
216 |
|
|
/* MBAinc points beyond picture dimensions */
|
217 |
|
|
if (!Quiet_Flag)
|
218 |
|
|
printf("Too many macroblocks in picture\n");
|
219 |
|
|
return(-1);
|
220 |
|
|
}
|
221 |
|
|
|
222 |
|
|
if (MBAinc==1) /* not skipped */
|
223 |
|
|
{
|
224 |
|
|
ret = decode_macroblock(¯oblock_type, &stwtype, &stwclass,
|
225 |
|
|
&motion_type, &dct_type, PMV, dc_dct_pred,
|
226 |
|
|
motion_vertical_field_select, dmvector);
|
227 |
|
|
|
228 |
|
|
if(ret==-1)
|
229 |
|
|
return(-1);
|
230 |
|
|
|
231 |
|
|
if(ret==0)
|
232 |
|
|
goto resync;
|
233 |
|
|
|
234 |
|
|
}
|
235 |
|
|
else /* MBAinc!=1: skipped macroblock */
|
236 |
|
|
{
|
237 |
|
|
/* ISO/IEC 13818-2 section 7.6.6 */
|
238 |
|
|
skipped_macroblock(dc_dct_pred, PMV, &motion_type,
|
239 |
|
|
motion_vertical_field_select, &stwtype, ¯oblock_type);
|
240 |
|
|
}
|
241 |
|
|
|
242 |
|
|
/* SCALABILITY: SNR */
|
243 |
|
|
/* ISO/IEC 13818-2 section 7.8 */
|
244 |
|
|
/* NOTE: we currently ignore faults encountered in this routine */
|
245 |
|
|
if (Two_Streams && enhan.scalable_mode==SC_SNR)
|
246 |
|
|
Decode_SNR_Macroblock(&SNRMBA, &SNRMBAinc, MBA, MBAmax, &dct_type);
|
247 |
|
|
|
248 |
|
|
/* ISO/IEC 13818-2 section 7.6 */
|
249 |
|
|
motion_compensation(MBA, macroblock_type, motion_type, PMV,
|
250 |
|
|
motion_vertical_field_select, dmvector, stwtype, dct_type);
|
251 |
|
|
|
252 |
|
|
|
253 |
|
|
/* advance to next macroblock */
|
254 |
|
|
MBA++;
|
255 |
|
|
MBAinc--;
|
256 |
|
|
|
257 |
|
|
/* SCALABILITY: SNR */
|
258 |
|
|
if (Two_Streams && enhan.scalable_mode==SC_SNR)
|
259 |
|
|
{
|
260 |
|
|
SNRMBA++;
|
261 |
|
|
SNRMBAinc--;
|
262 |
|
|
}
|
263 |
|
|
|
264 |
|
|
if (MBA>=MBAmax)
|
265 |
|
|
return(-1); /* all macroblocks decoded */
|
266 |
|
|
}
|
267 |
|
|
}
|
268 |
|
|
|
269 |
|
|
|
270 |
|
|
/* ISO/IEC 13818-2 section 6.3.17.1: Macroblock modes */
|
271 |
|
|
static void macroblock_modes(pmacroblock_type,pstwtype,pstwclass,
|
272 |
|
|
pmotion_type,pmotion_vector_count,pmv_format,pdmv,pmvscale,pdct_type)
|
273 |
|
|
int *pmacroblock_type, *pstwtype, *pstwclass;
|
274 |
|
|
int *pmotion_type, *pmotion_vector_count, *pmv_format, *pdmv, *pmvscale;
|
275 |
|
|
int *pdct_type;
|
276 |
|
|
{
|
277 |
|
|
int macroblock_type;
|
278 |
|
|
int stwtype, stwcode, stwclass;
|
279 |
|
|
int motion_type = 0;
|
280 |
|
|
int motion_vector_count, mv_format, dmv, mvscale;
|
281 |
|
|
int dct_type;
|
282 |
|
|
static unsigned char stwc_table[3][4]
|
283 |
|
|
= { {6,3,7,4}, {2,1,5,4}, {2,5,7,4} };
|
284 |
|
|
static unsigned char stwclass_table[9]
|
285 |
|
|
= {0, 1, 2, 1, 1, 2, 3, 3, 4};
|
286 |
|
|
|
287 |
|
|
/* get macroblock_type */
|
288 |
|
|
macroblock_type = Get_macroblock_type();
|
289 |
|
|
|
290 |
|
|
if (Fault_Flag) return;
|
291 |
|
|
|
292 |
|
|
/* get spatial_temporal_weight_code */
|
293 |
|
|
if (macroblock_type & MB_WEIGHT)
|
294 |
|
|
{
|
295 |
|
|
if (spatial_temporal_weight_code_table_index==0)
|
296 |
|
|
stwtype = 4;
|
297 |
|
|
else
|
298 |
|
|
{
|
299 |
|
|
stwcode = Get_Bits(2);
|
300 |
|
|
#ifdef TRACE
|
301 |
|
|
if (Trace_Flag)
|
302 |
|
|
{
|
303 |
|
|
printf("spatial_temporal_weight_code (");
|
304 |
|
|
Print_Bits(stwcode,2,2);
|
305 |
|
|
printf("): %d\n",stwcode);
|
306 |
|
|
}
|
307 |
|
|
#endif /* TRACE */
|
308 |
|
|
stwtype = stwc_table[spatial_temporal_weight_code_table_index-1][stwcode];
|
309 |
|
|
}
|
310 |
|
|
}
|
311 |
|
|
else
|
312 |
|
|
stwtype = (macroblock_type & MB_CLASS4) ? 8 : 0;
|
313 |
|
|
|
314 |
|
|
/* SCALABILITY: derive spatial_temporal_weight_class (Table 7-18) */
|
315 |
|
|
stwclass = stwclass_table[stwtype];
|
316 |
|
|
|
317 |
|
|
/* get frame/field motion type */
|
318 |
|
|
if (macroblock_type & (MACROBLOCK_MOTION_FORWARD|MACROBLOCK_MOTION_BACKWARD))
|
319 |
|
|
{
|
320 |
|
|
if (picture_structure==FRAME_PICTURE) /* frame_motion_type */
|
321 |
|
|
{
|
322 |
|
|
motion_type = frame_pred_frame_dct ? MC_FRAME : Get_Bits(2);
|
323 |
|
|
#ifdef TRACE
|
324 |
|
|
if (!frame_pred_frame_dct && Trace_Flag)
|
325 |
|
|
{
|
326 |
|
|
printf("frame_motion_type (");
|
327 |
|
|
Print_Bits(motion_type,2,2);
|
328 |
|
|
printf("): %s\n",motion_type==MC_FIELD?"Field MC":
|
329 |
|
|
motion_type==MC_FRAME?"Frame MC":
|
330 |
|
|
motion_type==MC_DMV?"Dual_Prime MC":"Invalid MC");
|
331 |
|
|
}
|
332 |
|
|
#endif /* TRACE */
|
333 |
|
|
}
|
334 |
|
|
else /* field_motion_type */
|
335 |
|
|
{
|
336 |
|
|
motion_type = Get_Bits(2);
|
337 |
|
|
#ifdef TRACE
|
338 |
|
|
if (Trace_Flag)
|
339 |
|
|
{
|
340 |
|
|
printf("field_motion_type (");
|
341 |
|
|
Print_Bits(motion_type,2,2);
|
342 |
|
|
printf("): %s\n",motion_type==MC_FIELD?"Field MC":
|
343 |
|
|
motion_type==MC_16X8?"16x8 MC":
|
344 |
|
|
motion_type==MC_DMV?"Dual_Prime MC":"Invalid MC");
|
345 |
|
|
}
|
346 |
|
|
#endif /* TRACE */
|
347 |
|
|
}
|
348 |
|
|
}
|
349 |
|
|
else if ((macroblock_type & MACROBLOCK_INTRA) && concealment_motion_vectors)
|
350 |
|
|
{
|
351 |
|
|
/* concealment motion vectors */
|
352 |
|
|
motion_type = (picture_structure==FRAME_PICTURE) ? MC_FRAME : MC_FIELD;
|
353 |
|
|
}
|
354 |
|
|
#if 0
|
355 |
|
|
else
|
356 |
|
|
{
|
357 |
|
|
printf("maroblock_modes(): unknown macroblock type\n");
|
358 |
|
|
motion_type = -1;
|
359 |
|
|
}
|
360 |
|
|
#endif
|
361 |
|
|
|
362 |
|
|
/* derive motion_vector_count, mv_format and dmv, (table 6-17, 6-18) */
|
363 |
|
|
if (picture_structure==FRAME_PICTURE)
|
364 |
|
|
{
|
365 |
|
|
motion_vector_count = (motion_type==MC_FIELD && stwclass<2) ? 2 : 1;
|
366 |
|
|
mv_format = (motion_type==MC_FRAME) ? MV_FRAME : MV_FIELD;
|
367 |
|
|
}
|
368 |
|
|
else
|
369 |
|
|
{
|
370 |
|
|
motion_vector_count = (motion_type==MC_16X8) ? 2 : 1;
|
371 |
|
|
mv_format = MV_FIELD;
|
372 |
|
|
}
|
373 |
|
|
|
374 |
|
|
dmv = (motion_type==MC_DMV); /* dual prime */
|
375 |
|
|
|
376 |
|
|
/* field mv predictions in frame pictures have to be scaled
|
377 |
|
|
* ISO/IEC 13818-2 section 7.6.3.1 Decoding the motion vectors
|
378 |
|
|
* IMPLEMENTATION: mvscale is derived for later use in motion_vectors()
|
379 |
|
|
* it displaces the stage:
|
380 |
|
|
*
|
381 |
|
|
* if((mv_format=="field")&&(t==1)&&(picture_structure=="Frame picture"))
|
382 |
|
|
* prediction = PMV[r][s][t] DIV 2;
|
383 |
|
|
*/
|
384 |
|
|
|
385 |
|
|
mvscale = ((mv_format==MV_FIELD) && (picture_structure==FRAME_PICTURE));
|
386 |
|
|
|
387 |
|
|
/* get dct_type (frame DCT / field DCT) */
|
388 |
|
|
dct_type = (picture_structure==FRAME_PICTURE)
|
389 |
|
|
&& (!frame_pred_frame_dct)
|
390 |
|
|
&& (macroblock_type & (MACROBLOCK_PATTERN|MACROBLOCK_INTRA))
|
391 |
|
|
? Get_Bits(1)
|
392 |
|
|
: 0;
|
393 |
|
|
|
394 |
|
|
#ifdef TRACE
|
395 |
|
|
if (Trace_Flag && (picture_structure==FRAME_PICTURE)
|
396 |
|
|
&& (!frame_pred_frame_dct)
|
397 |
|
|
&& (macroblock_type & (MACROBLOCK_PATTERN|MACROBLOCK_INTRA)))
|
398 |
|
|
printf("dct_type (%d): %s\n",dct_type,dct_type?"Field":"Frame");
|
399 |
|
|
#endif /* TRACE */
|
400 |
|
|
|
401 |
|
|
/* return values */
|
402 |
|
|
*pmacroblock_type = macroblock_type;
|
403 |
|
|
*pstwtype = stwtype;
|
404 |
|
|
*pstwclass = stwclass;
|
405 |
|
|
*pmotion_type = motion_type;
|
406 |
|
|
*pmotion_vector_count = motion_vector_count;
|
407 |
|
|
*pmv_format = mv_format;
|
408 |
|
|
*pdmv = dmv;
|
409 |
|
|
*pmvscale = mvscale;
|
410 |
|
|
*pdct_type = dct_type;
|
411 |
|
|
}
|
412 |
|
|
|
413 |
|
|
|
414 |
|
|
/* move/add 8x8-Block from block[comp] to backward_reference_frame */
|
415 |
|
|
/* copy reconstructed 8x8 block from block[comp] to current_frame[]
|
416 |
|
|
* ISO/IEC 13818-2 section 7.6.8: Adding prediction and coefficient data
|
417 |
|
|
* This stage also embodies some of the operations implied by:
|
418 |
|
|
* - ISO/IEC 13818-2 section 7.6.7: Combining predictions
|
419 |
|
|
* - ISO/IEC 13818-2 section 6.1.3: Macroblock
|
420 |
|
|
*/
|
421 |
|
|
static void Add_Block(comp,bx,by,dct_type,addflag)
|
422 |
|
|
int comp,bx,by,dct_type,addflag;
|
423 |
|
|
{
|
424 |
|
|
int cc,i, j, iincr;
|
425 |
|
|
unsigned char *rfp;
|
426 |
|
|
short *bp;
|
427 |
|
|
|
428 |
|
|
|
429 |
|
|
/* derive color component index */
|
430 |
|
|
/* equivalent to ISO/IEC 13818-2 Table 7-1 */
|
431 |
|
|
cc = (comp<4) ? 0 : (comp&1)+1; /* color component index */
|
432 |
|
|
|
433 |
|
|
if (cc==0)
|
434 |
|
|
{
|
435 |
|
|
/* luminance */
|
436 |
|
|
|
437 |
|
|
if (picture_structure==FRAME_PICTURE)
|
438 |
|
|
if (dct_type)
|
439 |
|
|
{
|
440 |
|
|
/* field DCT coding */
|
441 |
|
|
rfp = current_frame[0]
|
442 |
|
|
+ Coded_Picture_Width*(by+((comp&2)>>1)) + bx + ((comp&1)<<3);
|
443 |
|
|
iincr = (Coded_Picture_Width<<1) - 8;
|
444 |
|
|
}
|
445 |
|
|
else
|
446 |
|
|
{
|
447 |
|
|
/* frame DCT coding */
|
448 |
|
|
rfp = current_frame[0]
|
449 |
|
|
+ Coded_Picture_Width*(by+((comp&2)<<2)) + bx + ((comp&1)<<3);
|
450 |
|
|
iincr = Coded_Picture_Width - 8;
|
451 |
|
|
}
|
452 |
|
|
else
|
453 |
|
|
{
|
454 |
|
|
/* field picture */
|
455 |
|
|
rfp = current_frame[0]
|
456 |
|
|
+ (Coded_Picture_Width<<1)*(by+((comp&2)<<2)) + bx + ((comp&1)<<3);
|
457 |
|
|
iincr = (Coded_Picture_Width<<1) - 8;
|
458 |
|
|
}
|
459 |
|
|
}
|
460 |
|
|
else
|
461 |
|
|
{
|
462 |
|
|
/* chrominance */
|
463 |
|
|
|
464 |
|
|
/* scale coordinates */
|
465 |
|
|
if (chroma_format!=CHROMA444)
|
466 |
|
|
bx >>= 1;
|
467 |
|
|
if (chroma_format==CHROMA420)
|
468 |
|
|
by >>= 1;
|
469 |
|
|
if (picture_structure==FRAME_PICTURE)
|
470 |
|
|
{
|
471 |
|
|
if (dct_type && (chroma_format!=CHROMA420))
|
472 |
|
|
{
|
473 |
|
|
/* field DCT coding */
|
474 |
|
|
rfp = current_frame[cc]
|
475 |
|
|
+ Chroma_Width*(by+((comp&2)>>1)) + bx + (comp&8);
|
476 |
|
|
iincr = (Chroma_Width<<1) - 8;
|
477 |
|
|
}
|
478 |
|
|
else
|
479 |
|
|
{
|
480 |
|
|
/* frame DCT coding */
|
481 |
|
|
rfp = current_frame[cc]
|
482 |
|
|
+ Chroma_Width*(by+((comp&2)<<2)) + bx + (comp&8);
|
483 |
|
|
iincr = Chroma_Width - 8;
|
484 |
|
|
}
|
485 |
|
|
}
|
486 |
|
|
else
|
487 |
|
|
{
|
488 |
|
|
/* field picture */
|
489 |
|
|
rfp = current_frame[cc]
|
490 |
|
|
+ (Chroma_Width<<1)*(by+((comp&2)<<2)) + bx + (comp&8);
|
491 |
|
|
iincr = (Chroma_Width<<1) - 8;
|
492 |
|
|
}
|
493 |
|
|
}
|
494 |
|
|
|
495 |
|
|
bp = ld->block[comp];
|
496 |
|
|
|
497 |
|
|
if (addflag)
|
498 |
|
|
{
|
499 |
|
|
for (i=0; i<8; i++)
|
500 |
|
|
{
|
501 |
|
|
for (j=0; j<8; j++)
|
502 |
|
|
{
|
503 |
|
|
*rfp = Clip[*bp++ + *rfp];
|
504 |
|
|
rfp++;
|
505 |
|
|
}
|
506 |
|
|
|
507 |
|
|
rfp+= iincr;
|
508 |
|
|
}
|
509 |
|
|
}
|
510 |
|
|
else
|
511 |
|
|
{
|
512 |
|
|
for (i=0; i<8; i++)
|
513 |
|
|
{
|
514 |
|
|
for (j=0; j<8; j++)
|
515 |
|
|
*rfp++ = Clip[*bp++ + 128];
|
516 |
|
|
|
517 |
|
|
rfp+= iincr;
|
518 |
|
|
}
|
519 |
|
|
}
|
520 |
|
|
}
|
521 |
|
|
|
522 |
|
|
|
523 |
|
|
/* ISO/IEC 13818-2 section 7.8 */
|
524 |
|
|
static void Decode_SNR_Macroblock(SNRMBA, SNRMBAinc, MBA, MBAmax, dct_type)
|
525 |
|
|
int *SNRMBA, *SNRMBAinc;
|
526 |
|
|
int MBA, MBAmax;
|
527 |
|
|
int *dct_type;
|
528 |
|
|
{
|
529 |
|
|
int SNRmacroblock_type, SNRcoded_block_pattern, SNRdct_type, dummy;
|
530 |
|
|
int slice_vert_pos_ext, quantizer_scale_code, comp, code;
|
531 |
|
|
|
532 |
|
|
ld = &enhan;
|
533 |
|
|
|
534 |
|
|
if (*SNRMBAinc==0)
|
535 |
|
|
{
|
536 |
|
|
if (!Show_Bits(23)) /* next_start_code */
|
537 |
|
|
{
|
538 |
|
|
next_start_code();
|
539 |
|
|
code = Show_Bits(32);
|
540 |
|
|
|
541 |
|
|
if (codeSLICE_START_CODE_MAX)
|
542 |
|
|
{
|
543 |
|
|
/* only slice headers are allowed in picture_data */
|
544 |
|
|
if (!Quiet_Flag)
|
545 |
|
|
printf("SNR: Premature end of picture\n");
|
546 |
|
|
return;
|
547 |
|
|
}
|
548 |
|
|
|
549 |
|
|
Flush_Buffer32();
|
550 |
|
|
|
551 |
|
|
/* decode slice header (may change quantizer_scale) */
|
552 |
|
|
slice_vert_pos_ext = slice_header();
|
553 |
|
|
|
554 |
|
|
/* decode macroblock address increment */
|
555 |
|
|
*SNRMBAinc = Get_macroblock_address_increment();
|
556 |
|
|
|
557 |
|
|
/* set current location */
|
558 |
|
|
*SNRMBA =
|
559 |
|
|
((slice_vert_pos_ext<<7) + (code&255) - 1)*mb_width + *SNRMBAinc - 1;
|
560 |
|
|
|
561 |
|
|
*SNRMBAinc = 1; /* first macroblock in slice: not skipped */
|
562 |
|
|
}
|
563 |
|
|
else /* not next_start_code */
|
564 |
|
|
{
|
565 |
|
|
if (*SNRMBA>=MBAmax)
|
566 |
|
|
{
|
567 |
|
|
if (!Quiet_Flag)
|
568 |
|
|
printf("Too many macroblocks in picture\n");
|
569 |
|
|
return;
|
570 |
|
|
}
|
571 |
|
|
|
572 |
|
|
/* decode macroblock address increment */
|
573 |
|
|
*SNRMBAinc = Get_macroblock_address_increment();
|
574 |
|
|
}
|
575 |
|
|
}
|
576 |
|
|
|
577 |
|
|
if (*SNRMBA!=MBA)
|
578 |
|
|
{
|
579 |
|
|
/* streams out of sync */
|
580 |
|
|
if (!Quiet_Flag)
|
581 |
|
|
printf("Cant't synchronize streams\n");
|
582 |
|
|
return;
|
583 |
|
|
}
|
584 |
|
|
|
585 |
|
|
if (*SNRMBAinc==1) /* not skipped */
|
586 |
|
|
{
|
587 |
|
|
macroblock_modes(&SNRmacroblock_type, &dummy, &dummy,
|
588 |
|
|
&dummy, &dummy, &dummy, &dummy, &dummy,
|
589 |
|
|
&SNRdct_type);
|
590 |
|
|
|
591 |
|
|
if (SNRmacroblock_type & MACROBLOCK_PATTERN)
|
592 |
|
|
*dct_type = SNRdct_type;
|
593 |
|
|
|
594 |
|
|
if (SNRmacroblock_type & MACROBLOCK_QUANT)
|
595 |
|
|
{
|
596 |
|
|
quantizer_scale_code = Get_Bits(5);
|
597 |
|
|
ld->quantizer_scale =
|
598 |
|
|
ld->q_scale_type ? Non_Linear_quantizer_scale[quantizer_scale_code] : quantizer_scale_code<<1;
|
599 |
|
|
}
|
600 |
|
|
|
601 |
|
|
/* macroblock_pattern */
|
602 |
|
|
if (SNRmacroblock_type & MACROBLOCK_PATTERN)
|
603 |
|
|
{
|
604 |
|
|
SNRcoded_block_pattern = Get_coded_block_pattern();
|
605 |
|
|
|
606 |
|
|
if (chroma_format==CHROMA422)
|
607 |
|
|
SNRcoded_block_pattern = (SNRcoded_block_pattern<<2) | Get_Bits(2); /* coded_block_pattern_1 */
|
608 |
|
|
else if (chroma_format==CHROMA444)
|
609 |
|
|
SNRcoded_block_pattern = (SNRcoded_block_pattern<<6) | Get_Bits(6); /* coded_block_pattern_2 */
|
610 |
|
|
}
|
611 |
|
|
else
|
612 |
|
|
SNRcoded_block_pattern = 0;
|
613 |
|
|
|
614 |
|
|
/* decode blocks */
|
615 |
|
|
for (comp=0; comp
|
616 |
|
|
{
|
617 |
|
|
Clear_Block(comp);
|
618 |
|
|
|
619 |
|
|
if (SNRcoded_block_pattern & (1<<(block_count-1-comp)))
|
620 |
|
|
Decode_MPEG2_Non_Intra_Block(comp);
|
621 |
|
|
}
|
622 |
|
|
}
|
623 |
|
|
else /* SNRMBAinc!=1: skipped macroblock */
|
624 |
|
|
{
|
625 |
|
|
for (comp=0; comp
|
626 |
|
|
Clear_Block(comp);
|
627 |
|
|
}
|
628 |
|
|
|
629 |
|
|
ld = &base;
|
630 |
|
|
}
|
631 |
|
|
|
632 |
|
|
|
633 |
|
|
|
634 |
|
|
/* IMPLEMENTATION: set scratch pad macroblock to zero */
|
635 |
|
|
static void Clear_Block(comp)
|
636 |
|
|
int comp;
|
637 |
|
|
{
|
638 |
|
|
short *Block_Ptr;
|
639 |
|
|
int i;
|
640 |
|
|
|
641 |
|
|
Block_Ptr = ld->block[comp];
|
642 |
|
|
|
643 |
|
|
for (i=0; i<64; i++)
|
644 |
|
|
*Block_Ptr++ = 0;
|
645 |
|
|
}
|
646 |
|
|
|
647 |
|
|
|
648 |
|
|
/* SCALABILITY: add SNR enhancement layer block data to base layer */
|
649 |
|
|
/* ISO/IEC 13818-2 section 7.8.3.4: Addition of coefficients from the two layes */
|
650 |
|
|
static void Sum_Block(comp)
|
651 |
|
|
int comp;
|
652 |
|
|
{
|
653 |
|
|
short *Block_Ptr1, *Block_Ptr2;
|
654 |
|
|
int i;
|
655 |
|
|
|
656 |
|
|
Block_Ptr1 = base.block[comp];
|
657 |
|
|
Block_Ptr2 = enhan.block[comp];
|
658 |
|
|
|
659 |
|
|
for (i=0; i<64; i++)
|
660 |
|
|
*Block_Ptr1++ += *Block_Ptr2++;
|
661 |
|
|
}
|
662 |
|
|
|
663 |
|
|
|
664 |
|
|
/* limit coefficients to -2048..2047 */
|
665 |
|
|
/* ISO/IEC 13818-2 section 7.4.3 and 7.4.4: Saturation and Mismatch control */
|
666 |
|
|
static void Saturate(Block_Ptr)
|
667 |
|
|
short *Block_Ptr;
|
668 |
|
|
{
|
669 |
|
|
int i, j, k, sum, val;
|
670 |
|
|
|
671 |
|
|
sum = 0;
|
672 |
|
|
|
673 |
|
|
/* ISO/IEC 13818-2 section 7.4.3: Saturation */
|
674 |
|
|
for (i=0; i<64; i++)
|
675 |
|
|
{
|
676 |
|
|
val = Block_Ptr[i];
|
677 |
|
|
|
678 |
|
|
if (val>2047)
|
679 |
|
|
val = 2047;
|
680 |
|
|
else if (val<-2048)
|
681 |
|
|
val = -2048;
|
682 |
|
|
|
683 |
|
|
Block_Ptr[i] = val;
|
684 |
|
|
sum+= val;
|
685 |
|
|
}
|
686 |
|
|
|
687 |
|
|
/* ISO/IEC 13818-2 section 7.4.4: Mismatch control */
|
688 |
|
|
if ((sum&1)==0)
|
689 |
|
|
Block_Ptr[63]^= 1;
|
690 |
|
|
|
691 |
|
|
}
|
692 |
|
|
|
693 |
|
|
|
694 |
|
|
/* reuse old picture buffers as soon as they are no longer needed
|
695 |
|
|
based on life-time axioms of MPEG */
|
696 |
|
|
static void Update_Picture_Buffers()
|
697 |
|
|
{
|
698 |
|
|
int cc; /* color component index */
|
699 |
|
|
unsigned char *tmp; /* temporary swap pointer */
|
700 |
|
|
|
701 |
|
|
for (cc=0; cc<3; cc++)
|
702 |
|
|
{
|
703 |
|
|
/* B pictures do not need to be save for future reference */
|
704 |
|
|
if (picture_coding_type==B_TYPE)
|
705 |
|
|
{
|
706 |
|
|
current_frame[cc] = auxframe[cc];
|
707 |
|
|
#ifdef TRACE
|
708 |
|
|
if (Trace_Flag && (cc == 0))
|
709 |
|
|
printf("update_picture_buffers: current_frame = auxframe\n");
|
710 |
|
|
#endif /* TRACE */
|
711 |
|
|
}
|
712 |
|
|
else
|
713 |
|
|
{
|
714 |
|
|
/* only update at the beginning of the coded frame */
|
715 |
|
|
if (!Second_Field)
|
716 |
|
|
{
|
717 |
|
|
tmp = forward_reference_frame[cc];
|
718 |
|
|
|
719 |
|
|
/* the previously decoded reference frame is stored
|
720 |
|
|
coincident with the location where the backward
|
721 |
|
|
reference frame is stored (backwards prediction is not
|
722 |
|
|
needed in P pictures) */
|
723 |
|
|
forward_reference_frame[cc] = backward_reference_frame[cc];
|
724 |
|
|
|
725 |
|
|
/* update pointer for potential future B pictures */
|
726 |
|
|
backward_reference_frame[cc] = tmp;
|
727 |
|
|
#ifdef TRACE
|
728 |
|
|
if (Trace_Flag && (cc == 0))
|
729 |
|
|
printf("update_picture_buffers: swap forward_reference_frame and backward_reference_frame; current_frame = backward_reference_frame\n");
|
730 |
|
|
#endif /* TRACE */
|
731 |
|
|
}
|
732 |
|
|
else
|
733 |
|
|
{
|
734 |
|
|
#ifdef TRACE
|
735 |
|
|
if (Trace_Flag && (cc == 0))
|
736 |
|
|
printf("update_picture_buffers: don't swap forward_reference_frame and backward_reference_frame; current_frame = backward_reference_frame\n");
|
737 |
|
|
#endif /* TRACE */
|
738 |
|
|
}
|
739 |
|
|
|
740 |
|
|
/* can erase over old backward reference frame since it is not used
|
741 |
|
|
in a P picture, and since any subsequent B pictures will use the
|
742 |
|
|
previously decoded I or P frame as the backward_reference_frame */
|
743 |
|
|
current_frame[cc] = backward_reference_frame[cc];
|
744 |
|
|
}
|
745 |
|
|
|
746 |
|
|
#ifdef TRACE
|
747 |
|
|
if (Trace_Flag && (cc == 0))
|
748 |
|
|
printf("update_picture_buffers: forward_reference_frame: %x backward_reference_frame: %x auxframe: %x current_frame: %x\n",forward_reference_frame[0], backward_reference_frame[0], auxframe[0], current_frame[0]);
|
749 |
|
|
#endif /* TRACE */
|
750 |
|
|
|
751 |
|
|
/* IMPLEMENTATION:
|
752 |
|
|
one-time folding of a line offset into the pointer which stores the
|
753 |
|
|
memory address of the current frame saves offsets and conditional
|
754 |
|
|
branches throughout the remainder of the picture processing loop */
|
755 |
|
|
if (picture_structure==BOTTOM_FIELD)
|
756 |
|
|
current_frame[cc]+= (cc==0) ? Coded_Picture_Width : Chroma_Width;
|
757 |
|
|
}
|
758 |
|
|
}
|
759 |
|
|
|
760 |
|
|
|
761 |
|
|
/* store last frame */
|
762 |
|
|
|
763 |
|
|
void Output_Last_Frame_of_Sequence(Framenum)
|
764 |
|
|
int Framenum;
|
765 |
|
|
{
|
766 |
|
|
if (Second_Field)
|
767 |
|
|
printf("last frame incomplete, not stored\n");
|
768 |
|
|
else
|
769 |
|
|
Write_Frame(backward_reference_frame,Framenum-1);
|
770 |
|
|
}
|
771 |
|
|
|
772 |
|
|
|
773 |
|
|
|
774 |
|
|
static void frame_reorder(Bitstream_Framenum, Sequence_Framenum)
|
775 |
|
|
int Bitstream_Framenum, Sequence_Framenum;
|
776 |
|
|
{
|
777 |
|
|
/* tracking variables to insure proper output in spatial scalability */
|
778 |
|
|
static int Oldref_progressive_frame, Newref_progressive_frame;
|
779 |
|
|
|
780 |
|
|
if (Sequence_Framenum!=0)
|
781 |
|
|
{
|
782 |
|
|
if (picture_structure==FRAME_PICTURE || Second_Field)
|
783 |
|
|
{
|
784 |
|
|
if (picture_coding_type==B_TYPE)
|
785 |
|
|
Write_Frame(auxframe,Bitstream_Framenum-1);
|
786 |
|
|
else
|
787 |
|
|
{
|
788 |
|
|
Newref_progressive_frame = progressive_frame;
|
789 |
|
|
progressive_frame = Oldref_progressive_frame;
|
790 |
|
|
|
791 |
|
|
Write_Frame(forward_reference_frame,Bitstream_Framenum-1);
|
792 |
|
|
|
793 |
|
|
Oldref_progressive_frame = progressive_frame = Newref_progressive_frame;
|
794 |
|
|
}
|
795 |
|
|
}
|
796 |
|
|
#ifdef DISPLAY
|
797 |
|
|
else if (Output_Type==T_X11)
|
798 |
|
|
{
|
799 |
|
|
if(!Display_Progressive_Flag)
|
800 |
|
|
Display_Second_Field();
|
801 |
|
|
}
|
802 |
|
|
#endif
|
803 |
|
|
}
|
804 |
|
|
else
|
805 |
|
|
Oldref_progressive_frame = progressive_frame;
|
806 |
|
|
|
807 |
|
|
}
|
808 |
|
|
|
809 |
|
|
|
810 |
|
|
/* ISO/IEC 13818-2 section 7.6 */
|
811 |
|
|
static void motion_compensation(MBA, macroblock_type, motion_type, PMV,
|
812 |
|
|
motion_vertical_field_select, dmvector, stwtype, dct_type)
|
813 |
|
|
int MBA;
|
814 |
|
|
int macroblock_type;
|
815 |
|
|
int motion_type;
|
816 |
|
|
int PMV[2][2][2];
|
817 |
|
|
int motion_vertical_field_select[2][2];
|
818 |
|
|
int dmvector[2];
|
819 |
|
|
int stwtype;
|
820 |
|
|
int dct_type;
|
821 |
|
|
{
|
822 |
|
|
int bx, by;
|
823 |
|
|
int comp;
|
824 |
|
|
int j, k;
|
825 |
|
|
|
826 |
|
|
/* derive current macroblock position within picture */
|
827 |
|
|
/* ISO/IEC 13818-2 section 6.3.1.6 and 6.3.1.7 */
|
828 |
|
|
bx = 16*(MBA%mb_width);
|
829 |
|
|
by = 16*(MBA/mb_width);
|
830 |
|
|
|
831 |
|
|
/* motion compensation */
|
832 |
|
|
if (!(macroblock_type & MACROBLOCK_INTRA))
|
833 |
|
|
form_predictions(bx,by,macroblock_type,motion_type,PMV,
|
834 |
|
|
motion_vertical_field_select,dmvector,stwtype);
|
835 |
|
|
#ifdef TRACE
|
836 |
|
|
else if (Trace_Flag)
|
837 |
|
|
printf ("MC_NONE intra\n");
|
838 |
|
|
#endif
|
839 |
|
|
|
840 |
|
|
/* SCALABILITY: Data Partitioning */
|
841 |
|
|
if (base.scalable_mode==SC_DP)
|
842 |
|
|
ld = &base;
|
843 |
|
|
|
844 |
|
|
/* copy or add block data into picture */
|
845 |
|
|
for (comp=0; comp
|
846 |
|
|
{
|
847 |
|
|
/* SCALABILITY: SNR */
|
848 |
|
|
/* ISO/IEC 13818-2 section 7.8.3.4: Addition of coefficients from
|
849 |
|
|
the two a layers */
|
850 |
|
|
if (Two_Streams && enhan.scalable_mode==SC_SNR)
|
851 |
|
|
Sum_Block(comp); /* add SNR enhancement layer data to base layer */
|
852 |
|
|
|
853 |
|
|
/* MPEG-2 saturation and mismatch control */
|
854 |
|
|
/* base layer could be MPEG-1 stream, enhancement MPEG-2 SNR */
|
855 |
|
|
#ifdef TRACE_IDCT
|
856 |
|
|
if (Trace_Flag)
|
857 |
|
|
{
|
858 |
|
|
printf("before saturation and mismatch:\n");
|
859 |
|
|
for (j=0; j<8; j++)
|
860 |
|
|
{
|
861 |
|
|
for (k=0; k<8; k++)
|
862 |
|
|
printf(" %6d ", (ld->block[comp])[j * 8 + k]);
|
863 |
|
|
printf ("\n");
|
864 |
|
|
}
|
865 |
|
|
}
|
866 |
|
|
#endif /* TRACE */
|
867 |
|
|
/* ISO/IEC 13818-2 section 7.4.3 and 7.4.4: Saturation and Mismatch control */
|
868 |
|
|
if ((Two_Streams && enhan.scalable_mode==SC_SNR) || ld->MPEG2_Flag)
|
869 |
|
|
Saturate(ld->block[comp]);
|
870 |
|
|
|
871 |
|
|
#ifdef TRACE_IDCT
|
872 |
|
|
if (Trace_Flag)
|
873 |
|
|
{
|
874 |
|
|
printf("before idct:\n");
|
875 |
|
|
for (j=0; j<8; j++)
|
876 |
|
|
{
|
877 |
|
|
for (k=0; k<8; k++)
|
878 |
|
|
printf(" %6d ", (ld->block[comp])[j * 8 + k]);
|
879 |
|
|
printf ("\n");
|
880 |
|
|
}
|
881 |
|
|
}
|
882 |
|
|
#endif /* TRACE */
|
883 |
|
|
/* ISO/IEC 13818-2 section Annex A: inverse DCT */
|
884 |
|
|
if (Reference_IDCT_Flag)
|
885 |
|
|
#if HAVE_MMX
|
886 |
|
|
Reference_IDCT(ld->block[comp],(macroblock_type & MACROBLOCK_INTRA));
|
887 |
|
|
#else
|
888 |
|
|
Reference_IDCT(ld->block[comp]);
|
889 |
|
|
#endif
|
890 |
|
|
else
|
891 |
|
|
Fast_IDCT(ld->block[comp]);
|
892 |
|
|
|
893 |
|
|
#ifdef TRACE_IDCT
|
894 |
|
|
if (Trace_Flag)
|
895 |
|
|
{
|
896 |
|
|
printf("after idct:\n");
|
897 |
|
|
for (j=0; j<8; j++)
|
898 |
|
|
{
|
899 |
|
|
for (k=0; k<8; k++)
|
900 |
|
|
printf(" %6d ", (ld->block[comp])[j * 8 + k]);
|
901 |
|
|
printf ("\n");
|
902 |
|
|
}
|
903 |
|
|
}
|
904 |
|
|
#endif /* TRACE */
|
905 |
|
|
|
906 |
|
|
/* ISO/IEC 13818-2 section 7.6.8: Adding prediction and coefficient data */
|
907 |
|
|
Add_Block(comp,bx,by,dct_type,(macroblock_type & MACROBLOCK_INTRA)==0);
|
908 |
|
|
}
|
909 |
|
|
|
910 |
|
|
}
|
911 |
|
|
|
912 |
|
|
|
913 |
|
|
|
914 |
|
|
/* ISO/IEC 13818-2 section 7.6.6 */
|
915 |
|
|
static void skipped_macroblock(dc_dct_pred, PMV, motion_type,
|
916 |
|
|
motion_vertical_field_select, stwtype, macroblock_type)
|
917 |
|
|
int dc_dct_pred[3];
|
918 |
|
|
int PMV[2][2][2];
|
919 |
|
|
int *motion_type;
|
920 |
|
|
int motion_vertical_field_select[2][2];
|
921 |
|
|
int *stwtype;
|
922 |
|
|
int *macroblock_type;
|
923 |
|
|
{
|
924 |
|
|
int comp;
|
925 |
|
|
|
926 |
|
|
/* SCALABILITY: Data Paritioning */
|
927 |
|
|
if (base.scalable_mode==SC_DP)
|
928 |
|
|
ld = &base;
|
929 |
|
|
|
930 |
|
|
for (comp=0; comp
|
931 |
|
|
Clear_Block(comp);
|
932 |
|
|
|
933 |
|
|
/* reset intra_dc predictors */
|
934 |
|
|
/* ISO/IEC 13818-2 section 7.2.1: DC coefficients in intra blocks */
|
935 |
|
|
dc_dct_pred[0]=dc_dct_pred[1]=dc_dct_pred[2]=0;
|
936 |
|
|
|
937 |
|
|
/* reset motion vector predictors */
|
938 |
|
|
/* ISO/IEC 13818-2 section 7.6.3.4: Resetting motion vector predictors */
|
939 |
|
|
if (picture_coding_type==P_TYPE)
|
940 |
|
|
PMV[0][0][0]=PMV[0][0][1]=PMV[1][0][0]=PMV[1][0][1]=0;
|
941 |
|
|
#ifdef TRACE
|
942 |
|
|
if (Trace_Flag)
|
943 |
|
|
{
|
944 |
|
|
printf("resetting motion vectors: PMV[0..1][0][0..1] = 0 (skipped macroblock)\n");
|
945 |
|
|
}
|
946 |
|
|
#endif /* TRACE */
|
947 |
|
|
/* derive motion_type */
|
948 |
|
|
if (picture_structure==FRAME_PICTURE)
|
949 |
|
|
*motion_type = MC_FRAME;
|
950 |
|
|
else
|
951 |
|
|
{
|
952 |
|
|
*motion_type = MC_FIELD;
|
953 |
|
|
|
954 |
|
|
/* predict from field of same parity */
|
955 |
|
|
/* ISO/IEC 13818-2 section 7.6.6.1 and 7.6.6.3: P field picture and B field
|
956 |
|
|
picture */
|
957 |
|
|
motion_vertical_field_select[0][0]=motion_vertical_field_select[0][1] =
|
958 |
|
|
(picture_structure==BOTTOM_FIELD);
|
959 |
|
|
}
|
960 |
|
|
|
961 |
|
|
/* skipped I are spatial-only predicted, */
|
962 |
|
|
/* skipped P and B are temporal-only predicted */
|
963 |
|
|
/* ISO/IEC 13818-2 section 7.7.6: Skipped macroblocks */
|
964 |
|
|
*stwtype = (picture_coding_type==I_TYPE) ? 8 : 0;
|
965 |
|
|
|
966 |
|
|
/* IMPLEMENTATION: clear MACROBLOCK_INTRA */
|
967 |
|
|
*macroblock_type&= ~MACROBLOCK_INTRA;
|
968 |
|
|
|
969 |
|
|
}
|
970 |
|
|
|
971 |
|
|
|
972 |
|
|
/* return==-1 means go to next picture */
|
973 |
|
|
/* the expression "start of slice" is used throughout the normative
|
974 |
|
|
body of the MPEG specification */
|
975 |
|
|
static int start_of_slice(MBAmax, MBA, MBAinc,
|
976 |
|
|
dc_dct_pred, PMV)
|
977 |
|
|
int MBAmax;
|
978 |
|
|
int *MBA;
|
979 |
|
|
int *MBAinc;
|
980 |
|
|
int dc_dct_pred[3];
|
981 |
|
|
int PMV[2][2][2];
|
982 |
|
|
{
|
983 |
|
|
unsigned int code;
|
984 |
|
|
int slice_vert_pos_ext;
|
985 |
|
|
|
986 |
|
|
ld = &base;
|
987 |
|
|
|
988 |
|
|
Fault_Flag = 0;
|
989 |
|
|
|
990 |
|
|
next_start_code();
|
991 |
|
|
code = Show_Bits(32);
|
992 |
|
|
|
993 |
|
|
if (codeSLICE_START_CODE_MAX)
|
994 |
|
|
{
|
995 |
|
|
/* only slice headers are allowed in picture_data */
|
996 |
|
|
if (!Quiet_Flag)
|
997 |
|
|
printf("start_of_slice(): Premature end of picture\n");
|
998 |
|
|
|
999 |
|
|
return(-1); /* trigger: go to next picture */
|
1000 |
|
|
}
|
1001 |
|
|
|
1002 |
|
|
Flush_Buffer32();
|
1003 |
|
|
|
1004 |
|
|
/* decode slice header (may change quantizer_scale) */
|
1005 |
|
|
slice_vert_pos_ext = slice_header();
|
1006 |
|
|
|
1007 |
|
|
|
1008 |
|
|
/* SCALABILITY: Data Partitioning */
|
1009 |
|
|
if (base.scalable_mode==SC_DP)
|
1010 |
|
|
{
|
1011 |
|
|
ld = &enhan;
|
1012 |
|
|
next_start_code();
|
1013 |
|
|
code = Show_Bits(32);
|
1014 |
|
|
|
1015 |
|
|
if (codeSLICE_START_CODE_MAX)
|
1016 |
|
|
{
|
1017 |
|
|
/* only slice headers are allowed in picture_data */
|
1018 |
|
|
if (!Quiet_Flag)
|
1019 |
|
|
printf("DP: Premature end of picture\n");
|
1020 |
|
|
return(-1); /* trigger: go to next picture */
|
1021 |
|
|
}
|
1022 |
|
|
|
1023 |
|
|
Flush_Buffer32();
|
1024 |
|
|
|
1025 |
|
|
/* decode slice header (may change quantizer_scale) */
|
1026 |
|
|
slice_vert_pos_ext = slice_header();
|
1027 |
|
|
|
1028 |
|
|
if (base.priority_breakpoint!=1)
|
1029 |
|
|
ld = &base;
|
1030 |
|
|
}
|
1031 |
|
|
|
1032 |
|
|
/* decode macroblock address increment */
|
1033 |
|
|
*MBAinc = Get_macroblock_address_increment();
|
1034 |
|
|
|
1035 |
|
|
if (Fault_Flag)
|
1036 |
|
|
{
|
1037 |
|
|
printf("start_of_slice(): MBAinc unsuccessful\n");
|
1038 |
|
|
return(0); /* trigger: go to next slice */
|
1039 |
|
|
}
|
1040 |
|
|
|
1041 |
|
|
/* set current location */
|
1042 |
|
|
/* NOTE: the arithmetic used to derive macroblock_address below is
|
1043 |
|
|
* equivalent to ISO/IEC 13818-2 section 6.3.17: Macroblock
|
1044 |
|
|
*/
|
1045 |
|
|
*MBA = ((slice_vert_pos_ext<<7) + (code&255) - 1)*mb_width + *MBAinc - 1;
|
1046 |
|
|
*MBAinc = 1; /* first macroblock in slice: not skipped */
|
1047 |
|
|
|
1048 |
|
|
/* reset all DC coefficient and motion vector predictors */
|
1049 |
|
|
/* reset all DC coefficient and motion vector predictors */
|
1050 |
|
|
/* ISO/IEC 13818-2 section 7.2.1: DC coefficients in intra blocks */
|
1051 |
|
|
dc_dct_pred[0]=dc_dct_pred[1]=dc_dct_pred[2]=0;
|
1052 |
|
|
|
1053 |
|
|
/* ISO/IEC 13818-2 section 7.6.3.4: Resetting motion vector predictors */
|
1054 |
|
|
PMV[0][0][0]=PMV[0][0][1]=PMV[1][0][0]=PMV[1][0][1]=0;
|
1055 |
|
|
PMV[0][1][0]=PMV[0][1][1]=PMV[1][1][0]=PMV[1][1][1]=0;
|
1056 |
|
|
#ifdef TRACE
|
1057 |
|
|
if (Trace_Flag)
|
1058 |
|
|
{
|
1059 |
|
|
printf("resetting motion vectors: PMV[0..1][0..1][0..1] = 0\n");
|
1060 |
|
|
}
|
1061 |
|
|
#endif /* TRACE */
|
1062 |
|
|
|
1063 |
|
|
/* successfull: trigger decode macroblocks in slice */
|
1064 |
|
|
return(1);
|
1065 |
|
|
}
|
1066 |
|
|
|
1067 |
|
|
|
1068 |
|
|
/* ISO/IEC 13818-2 sections 7.2 through 7.5 */
|
1069 |
|
|
static int decode_macroblock(macroblock_type, stwtype, stwclass,
|
1070 |
|
|
motion_type, dct_type, PMV, dc_dct_pred,
|
1071 |
|
|
motion_vertical_field_select, dmvector)
|
1072 |
|
|
int *macroblock_type;
|
1073 |
|
|
int *stwtype;
|
1074 |
|
|
int *stwclass;
|
1075 |
|
|
int *motion_type;
|
1076 |
|
|
int *dct_type;
|
1077 |
|
|
int PMV[2][2][2];
|
1078 |
|
|
int dc_dct_pred[3];
|
1079 |
|
|
int motion_vertical_field_select[2][2];
|
1080 |
|
|
int dmvector[2];
|
1081 |
|
|
{
|
1082 |
|
|
/* locals */
|
1083 |
|
|
int quantizer_scale_code;
|
1084 |
|
|
int comp;
|
1085 |
|
|
|
1086 |
|
|
int motion_vector_count;
|
1087 |
|
|
int mv_format;
|
1088 |
|
|
int dmv;
|
1089 |
|
|
int mvscale;
|
1090 |
|
|
int coded_block_pattern;
|
1091 |
|
|
|
1092 |
|
|
/* SCALABILITY: Data Patitioning */
|
1093 |
|
|
if (base.scalable_mode==SC_DP)
|
1094 |
|
|
{
|
1095 |
|
|
if (base.priority_breakpoint<=2)
|
1096 |
|
|
ld = &enhan;
|
1097 |
|
|
else
|
1098 |
|
|
ld = &base;
|
1099 |
|
|
}
|
1100 |
|
|
|
1101 |
|
|
/* ISO/IEC 13818-2 section 6.3.17.1: Macroblock modes */
|
1102 |
|
|
macroblock_modes(macroblock_type, stwtype, stwclass,
|
1103 |
|
|
motion_type, &motion_vector_count, &mv_format, &dmv, &mvscale,
|
1104 |
|
|
dct_type);
|
1105 |
|
|
|
1106 |
|
|
if (Fault_Flag) return(0); /* trigger: go to next slice */
|
1107 |
|
|
|
1108 |
|
|
if (*macroblock_type & MACROBLOCK_QUANT)
|
1109 |
|
|
{
|
1110 |
|
|
quantizer_scale_code = Get_Bits(5);
|
1111 |
|
|
|
1112 |
|
|
#ifdef TRACE
|
1113 |
|
|
if (Trace_Flag)
|
1114 |
|
|
{
|
1115 |
|
|
printf("quantiser_scale_code (");
|
1116 |
|
|
Print_Bits(quantizer_scale_code,5,5);
|
1117 |
|
|
printf("): %d\n",quantizer_scale_code);
|
1118 |
|
|
}
|
1119 |
|
|
#endif /* TRACE */
|
1120 |
|
|
|
1121 |
|
|
/* ISO/IEC 13818-2 section 7.4.2.2: Quantizer scale factor */
|
1122 |
|
|
if (ld->MPEG2_Flag)
|
1123 |
|
|
ld->quantizer_scale =
|
1124 |
|
|
ld->q_scale_type ? Non_Linear_quantizer_scale[quantizer_scale_code]
|
1125 |
|
|
: (quantizer_scale_code << 1);
|
1126 |
|
|
else
|
1127 |
|
|
ld->quantizer_scale = quantizer_scale_code;
|
1128 |
|
|
|
1129 |
|
|
/* SCALABILITY: Data Partitioning */
|
1130 |
|
|
if (base.scalable_mode==SC_DP)
|
1131 |
|
|
/* make sure base.quantizer_scale is valid */
|
1132 |
|
|
base.quantizer_scale = ld->quantizer_scale;
|
1133 |
|
|
}
|
1134 |
|
|
|
1135 |
|
|
/* motion vectors */
|
1136 |
|
|
|
1137 |
|
|
|
1138 |
|
|
/* ISO/IEC 13818-2 section 6.3.17.2: Motion vectors */
|
1139 |
|
|
|
1140 |
|
|
/* decode forward motion vectors */
|
1141 |
|
|
if ((*macroblock_type & MACROBLOCK_MOTION_FORWARD)
|
1142 |
|
|
|| ((*macroblock_type & MACROBLOCK_INTRA)
|
1143 |
|
|
&& concealment_motion_vectors))
|
1144 |
|
|
{
|
1145 |
|
|
if (ld->MPEG2_Flag)
|
1146 |
|
|
motion_vectors(PMV,dmvector,motion_vertical_field_select,
|
1147 |
|
|
0,motion_vector_count,mv_format,f_code[0][0]-1,f_code[0][1]-1,
|
1148 |
|
|
dmv,mvscale);
|
1149 |
|
|
else
|
1150 |
|
|
motion_vector(PMV[0][0],dmvector,
|
1151 |
|
|
forward_f_code-1,forward_f_code-1,0,0,full_pel_forward_vector);
|
1152 |
|
|
}
|
1153 |
|
|
|
1154 |
|
|
if (Fault_Flag) return(0); /* trigger: go to next slice */
|
1155 |
|
|
|
1156 |
|
|
/* decode backward motion vectors */
|
1157 |
|
|
if (*macroblock_type & MACROBLOCK_MOTION_BACKWARD)
|
1158 |
|
|
{
|
1159 |
|
|
if (ld->MPEG2_Flag)
|
1160 |
|
|
motion_vectors(PMV,dmvector,motion_vertical_field_select,
|
1161 |
|
|
1,motion_vector_count,mv_format,f_code[1][0]-1,f_code[1][1]-1,0,
|
1162 |
|
|
mvscale);
|
1163 |
|
|
else
|
1164 |
|
|
motion_vector(PMV[0][1],dmvector,
|
1165 |
|
|
backward_f_code-1,backward_f_code-1,0,0,full_pel_backward_vector);
|
1166 |
|
|
}
|
1167 |
|
|
|
1168 |
|
|
if (Fault_Flag) return(0); /* trigger: go to next slice */
|
1169 |
|
|
|
1170 |
|
|
if ((*macroblock_type & MACROBLOCK_INTRA) && concealment_motion_vectors)
|
1171 |
|
|
Flush_Buffer(1); /* remove marker_bit */
|
1172 |
|
|
|
1173 |
|
|
if (base.scalable_mode==SC_DP && base.priority_breakpoint==3)
|
1174 |
|
|
ld = &enhan;
|
1175 |
|
|
|
1176 |
|
|
/* macroblock_pattern */
|
1177 |
|
|
/* ISO/IEC 13818-2 section 6.3.17.4: Coded block pattern */
|
1178 |
|
|
if (*macroblock_type & MACROBLOCK_PATTERN)
|
1179 |
|
|
{
|
1180 |
|
|
coded_block_pattern = Get_coded_block_pattern();
|
1181 |
|
|
|
1182 |
|
|
if (chroma_format==CHROMA422)
|
1183 |
|
|
{
|
1184 |
|
|
/* coded_block_pattern_1 */
|
1185 |
|
|
coded_block_pattern = (coded_block_pattern<<2) | Get_Bits(2);
|
1186 |
|
|
|
1187 |
|
|
#ifdef TRACE
|
1188 |
|
|
if (Trace_Flag)
|
1189 |
|
|
{
|
1190 |
|
|
printf("coded_block_pattern_1: ");
|
1191 |
|
|
Print_Bits(coded_block_pattern,2,2);
|
1192 |
|
|
printf(" (%d)\n",coded_block_pattern&3);
|
1193 |
|
|
}
|
1194 |
|
|
#endif /* TRACE */
|
1195 |
|
|
}
|
1196 |
|
|
else if (chroma_format==CHROMA444)
|
1197 |
|
|
{
|
1198 |
|
|
/* coded_block_pattern_2 */
|
1199 |
|
|
coded_block_pattern = (coded_block_pattern<<6) | Get_Bits(6);
|
1200 |
|
|
|
1201 |
|
|
#ifdef TRACE
|
1202 |
|
|
if (Trace_Flag)
|
1203 |
|
|
{
|
1204 |
|
|
printf("coded_block_pattern_2: ");
|
1205 |
|
|
Print_Bits(coded_block_pattern,6,6);
|
1206 |
|
|
printf(" (%d)\n",coded_block_pattern&63);
|
1207 |
|
|
}
|
1208 |
|
|
#endif /* TRACE */
|
1209 |
|
|
}
|
1210 |
|
|
}
|
1211 |
|
|
else
|
1212 |
|
|
coded_block_pattern = (*macroblock_type & MACROBLOCK_INTRA) ?
|
1213 |
|
|
(1<
|
1214 |
|
|
|
1215 |
|
|
if (Fault_Flag) return(0); /* trigger: go to next slice */
|
1216 |
|
|
|
1217 |
|
|
/* decode blocks */
|
1218 |
|
|
for (comp=0; comp
|
1219 |
|
|
{
|
1220 |
|
|
/* SCALABILITY: Data Partitioning */
|
1221 |
|
|
if (base.scalable_mode==SC_DP)
|
1222 |
|
|
ld = &base;
|
1223 |
|
|
|
1224 |
|
|
Clear_Block(comp);
|
1225 |
|
|
|
1226 |
|
|
if (coded_block_pattern & (1<<(block_count-1-comp)))
|
1227 |
|
|
{
|
1228 |
|
|
if (*macroblock_type & MACROBLOCK_INTRA)
|
1229 |
|
|
{
|
1230 |
|
|
if (ld->MPEG2_Flag)
|
1231 |
|
|
Decode_MPEG2_Intra_Block(comp,dc_dct_pred);
|
1232 |
|
|
else
|
1233 |
|
|
Decode_MPEG1_Intra_Block(comp,dc_dct_pred);
|
1234 |
|
|
}
|
1235 |
|
|
else
|
1236 |
|
|
{
|
1237 |
|
|
if (ld->MPEG2_Flag)
|
1238 |
|
|
Decode_MPEG2_Non_Intra_Block(comp);
|
1239 |
|
|
else
|
1240 |
|
|
Decode_MPEG1_Non_Intra_Block(comp);
|
1241 |
|
|
}
|
1242 |
|
|
|
1243 |
|
|
if (Fault_Flag) return(0); /* trigger: go to next slice */
|
1244 |
|
|
}
|
1245 |
|
|
#ifdef TRACE
|
1246 |
|
|
else
|
1247 |
|
|
{
|
1248 |
|
|
if (Trace_Flag) {
|
1249 |
|
|
printf("non-coded block\n");
|
1250 |
|
|
}
|
1251 |
|
|
}
|
1252 |
|
|
#endif
|
1253 |
|
|
}
|
1254 |
|
|
|
1255 |
|
|
if(picture_coding_type==D_TYPE)
|
1256 |
|
|
{
|
1257 |
|
|
/* remove end_of_macroblock (always 1, prevents startcode emulation) */
|
1258 |
|
|
/* ISO/IEC 11172-2 section 2.4.2.7 and 2.4.3.6 */
|
1259 |
|
|
marker_bit("D picture end_of_macroblock bit");
|
1260 |
|
|
}
|
1261 |
|
|
|
1262 |
|
|
/* reset intra_dc predictors */
|
1263 |
|
|
/* ISO/IEC 13818-2 section 7.2.1: DC coefficients in intra blocks */
|
1264 |
|
|
if (!(*macroblock_type & MACROBLOCK_INTRA))
|
1265 |
|
|
dc_dct_pred[0]=dc_dct_pred[1]=dc_dct_pred[2]=0;
|
1266 |
|
|
|
1267 |
|
|
/* reset motion vector predictors */
|
1268 |
|
|
if ((*macroblock_type & MACROBLOCK_INTRA) && !concealment_motion_vectors)
|
1269 |
|
|
{
|
1270 |
|
|
/* intra mb without concealment motion vectors */
|
1271 |
|
|
/* ISO/IEC 13818-2 section 7.6.3.4: Resetting motion vector predictors */
|
1272 |
|
|
PMV[0][0][0]=PMV[0][0][1]=PMV[1][0][0]=PMV[1][0][1]=0;
|
1273 |
|
|
PMV[0][1][0]=PMV[0][1][1]=PMV[1][1][0]=PMV[1][1][1]=0;
|
1274 |
|
|
#ifdef TRACE
|
1275 |
|
|
if (Trace_Flag)
|
1276 |
|
|
{
|
1277 |
|
|
printf("resetting motion vectors: PMV[0..1][0..1][0..1] = 0 (intra mb without concealment motion vectors)\n");
|
1278 |
|
|
}
|
1279 |
|
|
#endif /* TRACE */
|
1280 |
|
|
}
|
1281 |
|
|
|
1282 |
|
|
/* special "No_MC" macroblock_type case */
|
1283 |
|
|
/* ISO/IEC 13818-2 section 7.6.3.5: Prediction in P pictures */
|
1284 |
|
|
if ((picture_coding_type==P_TYPE)
|
1285 |
|
|
&& !(*macroblock_type & (MACROBLOCK_MOTION_FORWARD|MACROBLOCK_INTRA)))
|
1286 |
|
|
{
|
1287 |
|
|
/* non-intra mb without forward mv in a P picture */
|
1288 |
|
|
/* ISO/IEC 13818-2 section 7.6.3.4: Resetting motion vector predictors */
|
1289 |
|
|
PMV[0][0][0]=PMV[0][0][1]=PMV[1][0][0]=PMV[1][0][1]=0;
|
1290 |
|
|
#ifdef TRACE
|
1291 |
|
|
if (Trace_Flag)
|
1292 |
|
|
{
|
1293 |
|
|
printf("resetting motion vectors: PMV[0..1][0][0..1] = 0 (non-intra mb without forward mv in a P picture)\n");
|
1294 |
|
|
}
|
1295 |
|
|
#endif /* TRACE */
|
1296 |
|
|
|
1297 |
|
|
/* derive motion_type */
|
1298 |
|
|
/* ISO/IEC 13818-2 section 6.3.17.1: Macroblock modes, frame_motion_type */
|
1299 |
|
|
if (picture_structure==FRAME_PICTURE)
|
1300 |
|
|
*motion_type = MC_FRAME;
|
1301 |
|
|
else
|
1302 |
|
|
{
|
1303 |
|
|
*motion_type = MC_FIELD;
|
1304 |
|
|
/* predict from field of same parity */
|
1305 |
|
|
motion_vertical_field_select[0][0] = (picture_structure==BOTTOM_FIELD);
|
1306 |
|
|
}
|
1307 |
|
|
}
|
1308 |
|
|
|
1309 |
|
|
if (*stwclass==4)
|
1310 |
|
|
{
|
1311 |
|
|
/* purely spatially predicted macroblock */
|
1312 |
|
|
/* ISO/IEC 13818-2 section 7.7.5.1: Resetting motion vector predictions */
|
1313 |
|
|
PMV[0][0][0]=PMV[0][0][1]=PMV[1][0][0]=PMV[1][0][1]=0;
|
1314 |
|
|
PMV[0][1][0]=PMV[0][1][1]=PMV[1][1][0]=PMV[1][1][1]=0;
|
1315 |
|
|
#ifdef TRACE
|
1316 |
|
|
if (Trace_Flag)
|
1317 |
|
|
{
|
1318 |
|
|
printf("resetting motion vectors: PMV[0..1][0..1][0..1] = 0 (purely spatially predicted macroblock)\n");
|
1319 |
|
|
}
|
1320 |
|
|
#endif /* TRACE */
|
1321 |
|
|
}
|
1322 |
|
|
|
1323 |
|
|
/* successfully decoded macroblock */
|
1324 |
|
|
return(1);
|
1325 |
|
|
|
1326 |
|
|
} /* decode_macroblock */
|
1327 |
|
|
|
1328 |
|
|
|