OpenCores
URL https://opencores.org/ocsvn/mpeg2fpga/mpeg2fpga/trunk

Subversion Repositories mpeg2fpga

[/] [mpeg2fpga/] [trunk/] [tools/] [mpeg2dec/] [getpic.c] - Blame information for rev 2

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2019 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.