-
Notifications
You must be signed in to change notification settings - Fork 0
/
getpic.c
454 lines (375 loc) · 11.9 KB
/
getpic.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
/* getpic.c, picture decoding */
/*
* All modifications (mpeg2decode -> mpeg2play) are
* Copyright (C) 1994, Stefan Eckart. All Rights Reserved.
*/
/* Copyright (C) 1994, MPEG Software Simulation Group. All Rights Reserved. */
/*
* Disclaimer of Warranty
*
* These software programs are available to the user without any license fee or
* royalty on an "as is" basis. The MPEG Software Simulation Group disclaims
* any and all warranties, whether express, implied, or statuary, including any
* implied warranties or merchantability or of fitness for a particular
* purpose. In no event shall the copyright-holder be liable for any
* incidental, punitive, or consequential damages of any kind whatsoever
* arising from the use of these programs.
*
* This disclaimer of warranty extends to the user of these programs and user's
* customers, employees, agents, transferees, successors, and assigns.
*
* The MPEG Software Simulation Group does not represent or warrant that the
* programs furnished hereunder are free of infringement of any third-party
* patents.
*
* Commercial implementations of MPEG-1 and MPEG-2 video, including shareware,
* are subject to royalty fees to patent holders. Many of these patents are
* general enough such that they are unavoidable regardless of implementation
* design.
*
*/
#include <stdio.h>
#include "config.h"
#include "global.h"
/* private prototypes*/
static void getMBs _ANSI_ARGS_((int framenum));
static void macroblock_modes _ANSI_ARGS_((int *pmb_type, int *pstwtype,
int *pstwclass, int *pmotion_type, int *pmv_count, int *pmv_format, int *pdmv,
int *pmvscale, int *pdct_type));
/* decode one frame or field picture */
static int do_second_field = 0;
int getpicture(framenum)
int framenum;
{
if (pict_struct==FRAME_PICTURE && secondfield)
{
/* recover from illegal number of field pictures */
printf("odd number of field pictures\n");
secondfield = 0;
}
if(!secondfield)
assign_surfaces();
getMBs(framenum);
if(pict_struct==FRAME_PICTURE) {
if(bob) {
do_second_field = 1;
if(!display_frame(TOP_FIELD))
return 0;
} else {
if(!display_frame(FRAME_PICTURE))
return 0;
}
} else {
if(bob) {
if(secondfield) {
if(!display_frame(pict_struct))
return 0;
} else
display_field(pict_struct);
} else if(secondfield) {
if(!display_frame(FRAME_PICTURE))
return 0;
}
}
if (pict_struct!=FRAME_PICTURE)
secondfield = !secondfield;
return 1;
}
/* decode all macroblocks of the current picture */
static void getMBs(framenum)
int framenum;
{
int comp;
int MBA, MBAmax, MBAinc, mb_type, cbp, motion_type, dct_type;
int slice_vert_pos_ext;
unsigned int code;
int dc_dct_pred[3];
int mv_count, mv_format, mvscale;
int PMV[2][2][2], mv_field_sel[2][2];
int dmv, dmvector[2];
int qs;
int stwtype, stwclass;
/* number of macroblocks per picture */
MBAmax = mb_width*mb_height;
if (pict_struct!=FRAME_PICTURE)
MBAmax>>=1; /* field picture has half as mnay macroblocks as frame */
MBA = 0; /* macroblock address */
MBAinc = 0;
fault=0;
for (;;)
{
if(do_second_field && (MBA >= (MBAmax >> 1))) {
do_second_field = 0;
display_second_field(BOTTOM_FIELD);
}
if (MBAinc==0)
{
if (!showbits(23) || fault) /* startcode or fault */
{
resync: /* if fault: resynchronize to next startcode */
fault = 0;
if (MBA>=MBAmax)
return; /* all macroblocks decoded */
startcode();
code = showbits(32);
if (code<SLICE_MIN_START || code>SLICE_MAX_START)
{
/* only slice headers are allowed in picture_data */
if (!quiet)
printf("Premature end of picture\n");
return;
}
flushbits32();
/* decode slice header (may change quant_scale) */
slice_vert_pos_ext = getslicehdr();
/* decode macroblock address increment */
MBAinc = getMBA();
if (fault) goto resync;
/* set current location */
MBA = ((slice_vert_pos_ext<<7) + (code&255) - 1)*mb_width + MBAinc - 1;
MBAinc = 1; /* first macroblock in slice: not skipped */
/* reset all DC coefficient and motion vector predictors */
dc_dct_pred[0]=dc_dct_pred[1]=dc_dct_pred[2]=0;
PMV[0][0][0]=PMV[0][0][1]=PMV[1][0][0]=PMV[1][0][1]=0;
PMV[0][1][0]=PMV[0][1][1]=PMV[1][1][0]=PMV[1][1][1]=0;
}
else /* neither startcode nor fault */
{
if (MBA>=MBAmax)
{
if (!quiet)
printf("Too many macroblocks in picture\n");
return;
}
/* decode macroblock address increment */
MBAinc = getMBA();
if (fault) goto resync;
}
}
if (MBA>=MBAmax)
{
/* MBAinc points beyond picture dimensions */
if (!quiet)
printf("Too many macroblocks in picture\n");
return;
}
if (MBAinc==1) /* not skipped */
{
macroblock_modes(&mb_type, &stwtype, &stwclass,
&motion_type, &mv_count, &mv_format, &dmv, &mvscale,
&dct_type);
if (fault) goto resync;
if (mb_type & MB_QUANT)
{
qs = getbits(5);
if (ld->mpeg2)
ld->quant_scale =
ld->qscale_type ? non_linear_mquant_table[qs] : (qs << 1);
else
ld->quant_scale = qs;
}
/* motion vectors */
/* decode forward motion vectors */
if ((mb_type & MB_FORWARD) || ((mb_type & MB_INTRA) && conceal_mv))
{
if (ld->mpeg2)
motion_vectors(PMV,dmvector,mv_field_sel,
0,mv_count,mv_format,h_forw_r_size,v_forw_r_size,dmv,mvscale);
else
motion_vector(PMV[0][0],dmvector,
forw_r_size,forw_r_size,0,0,full_forw);
}
if (fault) goto resync;
/* decode backward motion vectors */
if (mb_type & MB_BACKWARD)
{
if (ld->mpeg2)
motion_vectors(PMV,dmvector,mv_field_sel,
1,mv_count,mv_format,h_back_r_size,v_back_r_size,0,mvscale);
else
motion_vector(PMV[0][1],dmvector,
back_r_size,back_r_size,0,0,full_back);
}
if (fault) goto resync;
if ((mb_type & MB_INTRA) && conceal_mv)
flushbits(1); /* remove marker_bit */
/* macroblock_pattern */
if (mb_type & MB_PATTERN)
{
cbp = getCBP();
if (chroma_format==CHROMA422)
{
cbp = (cbp<<2) | getbits(2); /* coded_block_pattern_1 */
}
else if (chroma_format==CHROMA444)
{
cbp = (cbp<<6) | getbits(6); /* coded_block_pattern_2 */
}
}
else
cbp = (mb_type & MB_INTRA) ? (1<<blk_cnt)-1 : 0;
if (fault) goto resync;
/* decode blocks */
for (comp=0; comp<blk_cnt; comp++) {
if (cbp & (1<<(blk_cnt-1-comp))) {
get_block();
if (mb_type & MB_INTRA)
{
if (ld->mpeg2)
getmpg2intrablock(comp,dc_dct_pred);
else
getintrablock(comp,dc_dct_pred);
}
else
{
if (ld->mpeg2)
getmpg2interblock(comp);
else
getinterblock(comp);
}
if (fault) goto resync;
add_block(mb_type & MB_INTRA);
}
}
/* reset intra_dc predictors */
if (!(mb_type & MB_INTRA))
dc_dct_pred[0]=dc_dct_pred[1]=dc_dct_pred[2]=0;
/* reset motion vector predictors */
if ((mb_type & MB_INTRA) && !conceal_mv)
{
/* intra mb without concealment motion vectors */
PMV[0][0][0]=PMV[0][0][1]=PMV[1][0][0]=PMV[1][0][1]=0;
PMV[0][1][0]=PMV[0][1][1]=PMV[1][1][0]=PMV[1][1][1]=0;
}
if ((pict_type==P_TYPE) && !(mb_type & (MB_FORWARD|MB_INTRA)))
{
/* non-intra mb without forward mv in a P picture */
PMV[0][0][0]=PMV[0][0][1]=PMV[1][0][0]=PMV[1][0][1]=0;
/* derive motion_type */
if (pict_struct==FRAME_PICTURE)
motion_type = MC_FRAME;
else
{
motion_type = MC_FIELD;
/* predict from field of same parity */
mv_field_sel[0][0] = (pict_struct==BOTTOM_FIELD);
}
}
if (stwclass==4)
{
/* purely spatially predicted macroblock */
PMV[0][0][0]=PMV[0][0][1]=PMV[1][0][0]=PMV[1][0][1]=0;
PMV[0][1][0]=PMV[0][1][1]=PMV[1][1][0]=PMV[1][1][1]=0;
}
}
else /* MBAinc!=1: skipped macroblock */
{
/* reset intra_dc predictors */
dc_dct_pred[0]=dc_dct_pred[1]=dc_dct_pred[2]=0;
/* reset motion vector predictors */
if (pict_type==P_TYPE)
PMV[0][0][0]=PMV[0][0][1]=PMV[1][0][0]=PMV[1][0][1]=0;
/* derive motion_type */
if (pict_struct==FRAME_PICTURE)
motion_type = MC_FRAME;
else
{
motion_type = MC_FIELD;
/* predict from field of same parity */
mv_field_sel[0][0]=mv_field_sel[0][1] = (pict_struct==BOTTOM_FIELD);
}
/* skipped I are spatial-only predicted, */
/* skipped P and B are temporal-only predicted */
stwtype = (pict_type==I_TYPE) ? 8 : 0;
/* clear MB_INTRA */
mb_type&= ~MB_INTRA;
cbp = 0; /* no block data */
}
/* motion compensation */
add_macroblock(MBA%mb_width, MBA/mb_width, mb_type, motion_type,
PMV, mv_field_sel, dmvector, cbp, dct_type);
/* advance to next macroblock */
MBA++;
MBAinc--;
}
}
static void macroblock_modes(pmb_type,pstwtype,pstwclass,
pmotion_type,pmv_count,pmv_format,pdmv,pmvscale,pdct_type)
int *pmb_type, *pstwtype, *pstwclass;
int *pmotion_type, *pmv_count, *pmv_format, *pdmv, *pmvscale;
int *pdct_type;
{
int mb_type;
int stwtype, stwcode, stwclass;
int motion_type = 0, mv_count, mv_format, dmv, mvscale;
int dct_type;
static unsigned char stwc_table[3][4]
= { {6,3,7,4}, {2,1,5,4}, {2,5,7,4} };
static unsigned char stwclass_table[9]
= {0, 1, 2, 1, 1, 2, 3, 3, 4};
/* get macroblock_type */
mb_type = getMBtype();
if (fault) return;
/* get spatial_temporal_weight_code */
if (mb_type & MB_WEIGHT)
{
if (stwc_table_index==0)
stwtype = 4;
else
{
stwcode = getbits(2);
stwtype = stwc_table[stwc_table_index-1][stwcode];
}
}
else
stwtype = (mb_type & MB_CLASS4) ? 8 : 0;
/* derive spatial_temporal_weight_class (Table 7-18) */
stwclass = stwclass_table[stwtype];
/* get frame/field motion type */
if (mb_type & (MB_FORWARD|MB_BACKWARD))
{
if (pict_struct==FRAME_PICTURE) /* frame_motion_type */
{
motion_type = frame_pred_dct ? MC_FRAME : getbits(2);
}
else /* field_motion_type */
{
motion_type = getbits(2);
}
}
else if ((mb_type & MB_INTRA) && conceal_mv)
{
/* concealment motion vectors */
motion_type = (pict_struct==FRAME_PICTURE) ? MC_FRAME : MC_FIELD;
}
/* derive mv_count, mv_format and dmv, (table 6-17, 6-18) */
if (pict_struct==FRAME_PICTURE)
{
mv_count = (motion_type==MC_FIELD && stwclass<2) ? 2 : 1;
mv_format = (motion_type==MC_FRAME) ? MV_FRAME : MV_FIELD;
}
else
{
mv_count = (motion_type==MC_16X8) ? 2 : 1;
mv_format = MV_FIELD;
}
dmv = (motion_type==MC_DMV); /* dual prime */
/* field mv predictions in frame pictures have to be scaled */
mvscale = ((mv_format==MV_FIELD) && (pict_struct==FRAME_PICTURE));
/* get dct_type (frame DCT / field DCT) */
dct_type = (pict_struct==FRAME_PICTURE)
&& (!frame_pred_dct)
&& (mb_type & (MB_PATTERN|MB_INTRA))
? getbits(1)
: 0;
/* return values */
*pmb_type = mb_type;
*pstwtype = stwtype;
*pstwclass = stwclass;
*pmotion_type = motion_type;
*pmv_count = mv_count;
*pmv_format = mv_format;
*pdmv = dmv;
*pmvscale = mvscale;
*pdct_type = dct_type;
}