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

Subversion Repositories mpeg2fpga

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

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 kdv
/* Predict.c, motion compensation routines                                    */
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
/* Detailed tracing */
37
static int printPixelAddress(unsigned char *addr, char *str, unsigned char *frame_addr, int w, int h)
38
{
39
    int size;
40
    long int offset, x, y;
41
    int val;
42
    size = w * h;
43
    offset = addr - frame_addr;
44
 
45
    if ((frame_addr != NULL) && (offset >= 0) && (offset < size)) {
46
        y = offset / w ;
47
        x = offset % w ;
48
        val = *addr;
49
//        printf("printPixelAddress: addr: %p frame_addr: %p w: %i h: %i offset: %li\n", addr, frame_addr, w, h, offset);
50
        printf("%s[%3li,%3li]", str, x, y);
51
        printf("(=%3i)", val);
52
        return (1);
53
    }
54
    else {
55
      return(0);
56
    }
57
}
58
 
59
void printPixel(unsigned char *addr)
60
{
61
  if (
62
  !printPixelAddress(addr, "bwd_y", backward_reference_frame[0], Coded_Picture_Width, Coded_Picture_Height) &&
63
  !printPixelAddress(addr, "fwd_y", forward_reference_frame[0], Coded_Picture_Width, Coded_Picture_Height) &&
64
  !printPixelAddress(addr, "aux_y", auxframe[0], Coded_Picture_Width, Coded_Picture_Height) &&
65
  !printPixelAddress(addr, "bwd_u", backward_reference_frame[1], Chroma_Width, Chroma_Height) &&
66
  !printPixelAddress(addr, "fwd_u", forward_reference_frame[1], Chroma_Width, Chroma_Height) &&
67
  !printPixelAddress(addr, "aux_u", auxframe[1], Chroma_Width, Chroma_Height) &&
68
  !printPixelAddress(addr, "bwd_v", backward_reference_frame[2], Chroma_Width, Chroma_Height) &&
69
  !printPixelAddress(addr, "fwd_v", forward_reference_frame[2], Chroma_Width, Chroma_Height) &&
70
  !printPixelAddress(addr, "aux_v", auxframe[2], Chroma_Width, Chroma_Height)) {
71
    printf ("***pixel not found***");
72
  }
73
}
74
#endif
75
 
76
/* private prototypes */
77
static void form_prediction _ANSI_ARGS_((unsigned char *src[], int sfield,
78
  unsigned char *dst[], int dfield,
79
  int lx, int lx2, int w, int h, int x, int y, int dx, int dy,
80
  int average_flag));
81
 
82
static void form_component_prediction _ANSI_ARGS_((unsigned char *src, unsigned char *dst,
83
  int lx, int lx2, int w, int h, int x, int y, int dx, int dy, int average_flag));
84
 
85
void form_predictions(bx,by,macroblock_type,motion_type,PMV,motion_vertical_field_select,dmvector,stwtype)
86
int bx, by;
87
int macroblock_type;
88
int motion_type;
89
int PMV[2][2][2], motion_vertical_field_select[2][2], dmvector[2];
90
int stwtype;
91
{
92
  int currentfield;
93
  unsigned char **predframe;
94
  int DMV[2][2];
95
  int stwtop, stwbot;
96
 
97
#ifdef TRACE
98
 
99
  char mc_descript[10];
100
  int mc_1_dst_field;
101
  int mc_1_fwd_valid;
102
  int mc_1_fwd_src_field;
103
  int mc_1_fwd_mv_x;
104
  int mc_1_fwd_mv_y;
105
  int mc_1_bwd_valid;
106
  int mc_1_bwd_src_field;
107
  int mc_1_bwd_mv_x;
108
  int mc_1_bwd_mv_y;
109
  int mc_2_dst_field;
110
  int mc_2_fwd_valid;
111
  int mc_2_fwd_src_field;
112
  int mc_2_fwd_mv_x;
113
  int mc_2_fwd_mv_y;
114
  int mc_2_bwd_valid;
115
  int mc_2_bwd_src_field;
116
  int mc_2_bwd_mv_x;
117
  int mc_2_bwd_mv_y;
118
 
119
  strcpy(mc_descript, "MC_NONE");
120
 
121
  mc_1_dst_field = 0;
122
  mc_1_fwd_valid = 0;
123
  mc_1_fwd_src_field = 0;
124
  mc_1_fwd_mv_x = 0;
125
  mc_1_fwd_mv_y = 0;
126
 
127
  mc_1_bwd_valid = 0;
128
  mc_1_bwd_src_field = 0;
129
  mc_1_bwd_mv_x = 0;
130
  mc_1_bwd_mv_y = 0;
131
 
132
  mc_2_dst_field = 0;
133
  mc_2_fwd_valid = 0;
134
  mc_2_fwd_src_field = 0;
135
  mc_2_fwd_mv_x = 0;
136
  mc_2_fwd_mv_y = 0;
137
 
138
  mc_2_bwd_valid = 0;
139
  mc_2_bwd_src_field = 0;
140
  mc_2_bwd_mv_x = 0;
141
  mc_2_bwd_mv_y = 0;
142
#endif
143
 
144
  stwtop = stwtype%3; /* 0:temporal, 1:(spat+temp)/2, 2:spatial */
145
  stwbot = stwtype/3;
146
 
147
  if ((macroblock_type & MACROBLOCK_MOTION_FORWARD)
148
   || (picture_coding_type==P_TYPE))
149
  {
150
    if (picture_structure==FRAME_PICTURE)
151
    {
152
      if ((motion_type==MC_FRAME)
153
        || !(macroblock_type & MACROBLOCK_MOTION_FORWARD))
154
      {
155
        /* frame-based prediction (broken into top and bottom halves
156
             for spatial scalability prediction purposes) */
157
        if (stwtop<2)
158
          form_prediction(forward_reference_frame,0,current_frame,0,
159
            Coded_Picture_Width,Coded_Picture_Width<<1,16,8,bx,by,
160
            PMV[0][0][0],PMV[0][0][1],stwtop);
161
 
162
        if (stwbot<2)
163
          form_prediction(forward_reference_frame,1,current_frame,1,
164
            Coded_Picture_Width,Coded_Picture_Width<<1,16,8,bx,by,
165
            PMV[0][0][0],PMV[0][0][1],stwbot);
166
 
167
#ifdef TRACE
168
       strcpy(mc_descript, "MC_FRAME");
169
 
170
       mc_1_dst_field = 0;
171
       mc_1_fwd_valid = 1;
172
       mc_1_fwd_src_field = 0;
173
       mc_1_fwd_mv_x = PMV[0][0][0];
174
       mc_1_fwd_mv_y = PMV[0][0][1];
175
 
176
       mc_2_dst_field = 1;
177
       mc_2_fwd_valid = 1;
178
       mc_2_fwd_src_field = 1;
179
       mc_2_fwd_mv_x = PMV[0][0][0];
180
       mc_2_fwd_mv_y = PMV[0][0][1];
181
#endif
182
      }
183
      else if (motion_type==MC_FIELD) /* field-based prediction */
184
      {
185
        /* top field prediction */
186
        if (stwtop<2)
187
          form_prediction(forward_reference_frame,motion_vertical_field_select[0][0],
188
            current_frame,0,Coded_Picture_Width<<1,Coded_Picture_Width<<1,16,8,
189
            bx,by>>1,PMV[0][0][0],PMV[0][0][1]>>1,stwtop);
190
 
191
        /* bottom field prediction */
192
        if (stwbot<2)
193
          form_prediction(forward_reference_frame,motion_vertical_field_select[1][0],
194
            current_frame,1,Coded_Picture_Width<<1,Coded_Picture_Width<<1,16,8,
195
            bx,by>>1,PMV[1][0][0],PMV[1][0][1]>>1,stwbot);
196
 
197
#ifdef TRACE
198
       strcpy(mc_descript, "MC_FIELD");
199
 
200
       mc_1_dst_field = 0;
201
       mc_1_fwd_valid = 1;
202
       mc_1_fwd_src_field = motion_vertical_field_select[0][0];
203
       mc_1_fwd_mv_x = PMV[0][0][0];
204
       mc_1_fwd_mv_y = PMV[0][0][1]>>1;
205
 
206
       mc_2_dst_field = 1;
207
       mc_2_fwd_valid = 1;
208
       mc_2_fwd_src_field = motion_vertical_field_select[1][0];
209
       mc_2_fwd_mv_x = PMV[1][0][0];
210
       mc_2_fwd_mv_y = PMV[1][0][1]>>1;
211
#endif
212
      }
213
      else if (motion_type==MC_DMV) /* dual prime prediction */
214
      {
215
        /* calculate derived motion vectors */
216
        Dual_Prime_Arithmetic(DMV,dmvector,PMV[0][0][0],PMV[0][0][1]>>1);
217
 
218
        if (stwtop<2)
219
        {
220
          /* predict top field from top field */
221
          form_prediction(forward_reference_frame,0,current_frame,0,
222
            Coded_Picture_Width<<1,Coded_Picture_Width<<1,16,8,bx,by>>1,
223
            PMV[0][0][0],PMV[0][0][1]>>1,0);
224
 
225
          /* predict and add to top field from bottom field */
226
          form_prediction(forward_reference_frame,1,current_frame,0,
227
            Coded_Picture_Width<<1,Coded_Picture_Width<<1,16,8,bx,by>>1,
228
            DMV[0][0],DMV[0][1],1);
229
        }
230
 
231
        if (stwbot<2)
232
        {
233
          /* predict bottom field from bottom field */
234
          form_prediction(forward_reference_frame,1,current_frame,1,
235
            Coded_Picture_Width<<1,Coded_Picture_Width<<1,16,8,bx,by>>1,
236
            PMV[0][0][0],PMV[0][0][1]>>1,0);
237
 
238
          /* predict and add to bottom field from top field */
239
          form_prediction(forward_reference_frame,0,current_frame,1,
240
            Coded_Picture_Width<<1,Coded_Picture_Width<<1,16,8,bx,by>>1,
241
            DMV[1][0],DMV[1][1],1);
242
        }
243
 
244
#ifdef TRACE
245
       strcpy(mc_descript, "MC_DMV");
246
 
247
       mc_1_dst_field = 0;
248
       mc_1_fwd_valid = 1;
249
       mc_1_fwd_src_field = 0;
250
       mc_1_fwd_mv_x = PMV[0][0][0];
251
       mc_1_fwd_mv_y = PMV[0][0][1]>>1;
252
 
253
       mc_1_bwd_valid = 1;
254
       mc_1_bwd_src_field = 1;
255
       mc_1_bwd_mv_x = DMV[0][0];
256
       mc_1_bwd_mv_y = DMV[0][1];
257
 
258
       mc_2_dst_field = 1;
259
       mc_2_fwd_valid = 1;
260
       mc_2_fwd_src_field = 1;
261
       mc_2_fwd_mv_x = PMV[0][0][0];
262
       mc_2_fwd_mv_y = PMV[0][0][1]>>1;
263
 
264
       mc_2_bwd_valid = 1;
265
       mc_2_bwd_src_field = 0;
266
       mc_2_bwd_mv_x = DMV[1][0];
267
       mc_2_bwd_mv_y = DMV[1][1];
268
#endif
269
      }
270
      else
271
      {
272
        /* invalid motion_type */
273
        printf("invalid motion_type\n");
274
#ifdef TRACE
275
       strcpy(mc_descript, "MC_ERR");
276
#endif
277
      }
278
    }
279
    else /* TOP_FIELD or BOTTOM_FIELD */
280
    {
281
      /* field picture */
282
      currentfield = (picture_structure==BOTTOM_FIELD);
283
 
284
      /* determine which frame to use for prediction */
285
      if ((picture_coding_type==P_TYPE) && Second_Field
286
         && (currentfield!=motion_vertical_field_select[0][0]))
287
        predframe = backward_reference_frame; /* same frame */
288
      else
289
        predframe = forward_reference_frame; /* previous frame */
290
 
291
      if ((motion_type==MC_FIELD)
292
        || !(macroblock_type & MACROBLOCK_MOTION_FORWARD))
293
      {
294
        /* field-based prediction */
295
        if (stwtop<2)
296
          form_prediction(predframe,motion_vertical_field_select[0][0],current_frame,0,
297
            Coded_Picture_Width<<1,Coded_Picture_Width<<1,16,16,bx,by,
298
            PMV[0][0][0],PMV[0][0][1],stwtop);
299
 
300
#ifdef TRACE
301
       strcpy(mc_descript, "MC_FIELD");
302
 
303
       mc_1_dst_field = currentfield;
304
       mc_1_fwd_valid = 1;
305
       mc_1_fwd_src_field = motion_vertical_field_select[0][0];
306
       mc_1_fwd_mv_x = PMV[0][0][0];
307
       mc_1_fwd_mv_y = PMV[0][0][1];
308
#endif
309
      }
310
      else if (motion_type==MC_16X8)
311
      {
312
        if (stwtop<2)
313
        {
314
          form_prediction(predframe,motion_vertical_field_select[0][0],current_frame,0,
315
            Coded_Picture_Width<<1,Coded_Picture_Width<<1,16,8,bx,by,
316
            PMV[0][0][0],PMV[0][0][1],stwtop);
317
 
318
          /* determine which frame to use for lower half prediction */
319
          if ((picture_coding_type==P_TYPE) && Second_Field
320
             && (currentfield!=motion_vertical_field_select[1][0]))
321
            predframe = backward_reference_frame; /* same frame */
322
          else
323
            predframe = forward_reference_frame; /* previous frame */
324
 
325
          form_prediction(predframe,motion_vertical_field_select[1][0],current_frame,0,
326
            Coded_Picture_Width<<1,Coded_Picture_Width<<1,16,8,bx,by+8,
327
            PMV[1][0][0],PMV[1][0][1],stwtop);
328
        }
329
 
330
#ifdef TRACE
331
       strcpy(mc_descript, "MC_16X8");
332
 
333
       mc_1_dst_field = currentfield;
334
       mc_1_fwd_valid = 1;
335
       mc_1_fwd_src_field = motion_vertical_field_select[0][0];
336
       mc_1_fwd_mv_x = PMV[0][0][0];
337
       mc_1_fwd_mv_y = PMV[0][0][1];
338
 
339
       mc_1_bwd_valid = 1;
340
       mc_1_bwd_src_field = motion_vertical_field_select[1][0];
341
       mc_1_bwd_mv_x = PMV[1][0][0];
342
       mc_1_bwd_mv_y = PMV[1][0][1];
343
#endif
344
      }
345
      else if (motion_type==MC_DMV) /* dual prime prediction */
346
      {
347
        if (Second_Field)
348
          predframe = backward_reference_frame; /* same frame */
349
        else
350
          predframe = forward_reference_frame; /* previous frame */
351
 
352
        /* calculate derived motion vectors */
353
        Dual_Prime_Arithmetic(DMV,dmvector,PMV[0][0][0],PMV[0][0][1]);
354
 
355
        /* predict from field of same parity */
356
        form_prediction(forward_reference_frame,currentfield,current_frame,0,
357
          Coded_Picture_Width<<1,Coded_Picture_Width<<1,16,16,bx,by,
358
          PMV[0][0][0],PMV[0][0][1],0);
359
 
360
        /* predict from field of opposite parity */
361
        form_prediction(predframe,!currentfield,current_frame,0,
362
          Coded_Picture_Width<<1,Coded_Picture_Width<<1,16,16,bx,by,
363
          DMV[0][0],DMV[0][1],1);
364
 
365
#ifdef TRACE
366
       strcpy(mc_descript, "MC_DMV");
367
 
368
       mc_1_dst_field = currentfield;
369
       mc_1_fwd_valid = 1;
370
       mc_1_fwd_src_field = currentfield;
371
       mc_1_fwd_mv_x = PMV[0][0][0];
372
       mc_1_fwd_mv_y = PMV[0][0][1];
373
 
374
       mc_1_bwd_valid = 1;
375
       mc_1_bwd_src_field = !currentfield;
376
       mc_1_bwd_mv_x = DMV[0][0];
377
       mc_1_bwd_mv_y = DMV[0][1];
378
#endif
379
      }
380
      else
381
      {
382
        /* invalid motion_type */
383
        printf("invalid motion_type\n");
384
#ifdef TRACE
385
       strcpy(mc_descript, "MC_ERR");
386
#endif
387
      }
388
    }
389
    stwtop = stwbot = 1;
390
  }
391
 
392
  if (macroblock_type & MACROBLOCK_MOTION_BACKWARD)
393
  {
394
    if (picture_structure==FRAME_PICTURE)
395
    {
396
      if (motion_type==MC_FRAME)
397
      {
398
        /* frame-based prediction */
399
        if (stwtop<2)
400
          form_prediction(backward_reference_frame,0,current_frame,0,
401
            Coded_Picture_Width,Coded_Picture_Width<<1,16,8,bx,by,
402
            PMV[0][1][0],PMV[0][1][1],stwtop);
403
 
404
        if (stwbot<2)
405
          form_prediction(backward_reference_frame,1,current_frame,1,
406
            Coded_Picture_Width,Coded_Picture_Width<<1,16,8,bx,by,
407
            PMV[0][1][0],PMV[0][1][1],stwbot);
408
 
409
#ifdef TRACE
410
       strcpy(mc_descript, "MC_FRAME");
411
 
412
       mc_1_dst_field = 0;
413
 
414
       mc_1_bwd_valid = 1;
415
       mc_1_bwd_src_field = 0;
416
       mc_1_bwd_mv_x = PMV[0][1][0];
417
       mc_1_bwd_mv_y = PMV[0][1][1];
418
 
419
       mc_2_dst_field = 1;
420
 
421
       mc_2_bwd_valid = 1;
422
       mc_2_bwd_src_field = 1;
423
       mc_2_bwd_mv_x = PMV[0][1][0];
424
       mc_2_bwd_mv_y = PMV[0][1][1];
425
#endif
426
      }
427
      else /* field-based prediction */
428
      {
429
        /* top field prediction */
430
        if (stwtop<2)
431
          form_prediction(backward_reference_frame,motion_vertical_field_select[0][1],
432
            current_frame,0,Coded_Picture_Width<<1,Coded_Picture_Width<<1,16,8,
433
            bx,by>>1,PMV[0][1][0],PMV[0][1][1]>>1,stwtop);
434
 
435
        /* bottom field prediction */
436
        if (stwbot<2)
437
          form_prediction(backward_reference_frame,motion_vertical_field_select[1][1],
438
            current_frame,1,Coded_Picture_Width<<1,Coded_Picture_Width<<1,16,8,
439
            bx,by>>1,PMV[1][1][0],PMV[1][1][1]>>1,stwbot);
440
 
441
#ifdef TRACE
442
       strcpy(mc_descript, "MC_FIELD");
443
 
444
       mc_1_dst_field = 0;
445
 
446
       mc_1_bwd_valid = 1;
447
       mc_1_bwd_src_field = motion_vertical_field_select[0][1];
448
       mc_1_bwd_mv_x = PMV[0][1][0];
449
       mc_1_bwd_mv_y = PMV[0][1][1]>>1;
450
 
451
       mc_2_dst_field = 1;
452
 
453
       mc_2_bwd_valid = 1;
454
       mc_2_bwd_src_field = motion_vertical_field_select[1][1];
455
       mc_2_bwd_mv_x = PMV[1][1][0];
456
       mc_2_bwd_mv_y = PMV[1][1][1]>>1;
457
#endif
458
      }
459
    }
460
    else /* TOP_FIELD or BOTTOM_FIELD */
461
    {
462
      /* field picture */
463
      if (motion_type==MC_FIELD)
464
      {
465
        /* field-based prediction */
466
        form_prediction(backward_reference_frame,motion_vertical_field_select[0][1],
467
          current_frame,0,Coded_Picture_Width<<1,Coded_Picture_Width<<1,16,16,
468
          bx,by,PMV[0][1][0],PMV[0][1][1],stwtop);
469
 
470
#ifdef TRACE
471
       strcpy(mc_descript, "MC_FIELD");
472
 
473
       mc_1_dst_field = currentfield;
474
 
475
       mc_1_bwd_valid = 1;
476
       mc_1_bwd_src_field = motion_vertical_field_select[0][1];
477
       mc_1_bwd_mv_x = PMV[0][1][0];
478
       mc_1_bwd_mv_y = PMV[0][1][1];
479
#endif
480
      }
481
      else if (motion_type==MC_16X8)
482
      {
483
        form_prediction(backward_reference_frame,motion_vertical_field_select[0][1],
484
          current_frame,0,Coded_Picture_Width<<1,Coded_Picture_Width<<1,16,8,
485
          bx,by,PMV[0][1][0],PMV[0][1][1],stwtop);
486
 
487
        form_prediction(backward_reference_frame,motion_vertical_field_select[1][1],
488
          current_frame,0,Coded_Picture_Width<<1,Coded_Picture_Width<<1,16,8,
489
          bx,by+8,PMV[1][1][0],PMV[1][1][1],stwtop);
490
 
491
#ifdef TRACE
492
       strcpy(mc_descript, "MC_16X8");
493
 
494
       mc_1_dst_field = currentfield;
495
 
496
       mc_1_bwd_valid = 1;
497
       mc_1_bwd_src_field = motion_vertical_field_select[0][1];
498
       mc_1_bwd_mv_x = PMV[0][1][0];
499
       mc_1_bwd_mv_y = PMV[0][1][1];
500
 
501
       mc_2_dst_field = currentfield;
502
 
503
       mc_2_bwd_valid = 1;
504
       mc_2_bwd_src_field = motion_vertical_field_select[1][1];
505
       mc_2_bwd_mv_x = PMV[1][1][0];
506
       mc_2_bwd_mv_y = PMV[1][1][1];
507
#endif
508
      }
509
      else
510
      {
511
        /* invalid motion_type */
512
        printf("invalid motion_type\n");
513
#ifdef TRACE
514
       strcpy(mc_descript, "MC_ERR");
515
#endif
516
      }
517
    }
518
  }
519
#ifdef TRACE
520
  if (Trace_Flag && (mc_1_fwd_valid || mc_1_bwd_valid))
521
  {
522
    printf("field %1i %10s", mc_1_dst_field, mc_descript);
523
    if (mc_1_fwd_valid) printf (" fwd field %1i (%5i, %5i)", mc_1_fwd_src_field, mc_1_fwd_mv_x, mc_1_fwd_mv_y);
524
    if (mc_1_bwd_valid) printf (" bwd field %1i (%5i, %5i)", mc_1_bwd_src_field, mc_1_bwd_mv_x, mc_1_bwd_mv_y);
525
    printf("\n");
526
  }
527
 
528
  if (Trace_Flag && (mc_2_fwd_valid || mc_2_bwd_valid))
529
  {
530
    printf("field %1i %10s", mc_2_dst_field, mc_descript);
531
    if (mc_2_fwd_valid) printf (" fwd field %1i (%5i, %5i)", mc_2_fwd_src_field, mc_2_fwd_mv_x, mc_2_fwd_mv_y);
532
    if (mc_2_bwd_valid) printf (" bwd field %1i (%5i, %5i)", mc_2_bwd_src_field, mc_2_bwd_mv_x, mc_2_bwd_mv_y);
533
    printf("\n");
534
  }
535
#endif
536
}
537
 
538
static void form_prediction(src,sfield,dst,dfield,lx,lx2,w,h,x,y,dx,dy,average_flag)
539
unsigned char *src[]; /* prediction source buffer */
540
int sfield;           /* prediction source field number (0 or 1) */
541
unsigned char *dst[]; /* prediction destination buffer */
542
int dfield;           /* prediction destination field number (0 or 1)*/
543
int lx,lx2;           /* line strides */
544
int w,h;              /* prediction block/sub-block width, height */
545
int x,y;              /* pixel co-ordinates of top-left sample in current MB */
546
int dx,dy;            /* horizontal, vertical prediction address */
547
int average_flag;     /* add prediction error to prediction ? */
548
{
549
  /* Y */
550
  form_component_prediction(src[0]+(sfield?lx2>>1:0),dst[0]+(dfield?lx2>>1:0),
551
    lx,lx2,w,h,x,y,dx,dy,average_flag);
552
 
553
  if (chroma_format!=CHROMA444)
554
  {
555
    lx>>=1; lx2>>=1; w>>=1; x>>=1; dx/=2;
556
  }
557
 
558
  if (chroma_format==CHROMA420)
559
  {
560
    h>>=1; y>>=1; dy/=2;
561
  }
562
 
563
  /* Cb */
564
  form_component_prediction(src[1]+(sfield?lx2>>1:0),dst[1]+(dfield?lx2>>1:0),
565
    lx,lx2,w,h,x,y,dx,dy,average_flag);
566
 
567
  /* Cr */
568
  form_component_prediction(src[2]+(sfield?lx2>>1:0),dst[2]+(dfield?lx2>>1:0),
569
    lx,lx2,w,h,x,y,dx,dy,average_flag);
570
}
571
 
572
/* ISO/IEC 13818-2 section 7.6.4: Forming predictions */
573
/* NOTE: the arithmetic below produces numerically equivalent results
574
 *  to 7.6.4, yet is more elegant. It differs in the following ways:
575
 *
576
 *   1. the vectors (dx, dy) are based on cartesian frame
577
 *      coordiantes along a half-pel grid (always positive numbers)
578
 *      In contrast, vector[r][s][t] are differential (with positive and
579
 *      negative values). As a result, deriving the integer vectors
580
 *      (int_vec[t]) from dx, dy is accomplished by a simple right shift.
581
 *
582
 *   2. Half pel flags (xh, yh) are equivalent to the LSB (Least
583
 *      Significant Bit) of the half-pel coordinates (dx,dy).
584
 *
585
 *
586
 *  NOTE: the work of combining predictions (ISO/IEC 13818-2 section 7.6.7)
587
 *  is distributed among several other stages.  This is accomplished by
588
 *  folding line offsets into the source and destination (src,dst)
589
 *  addresses (note the call arguments to form_prediction() in Predict()),
590
 *  line stride variables lx and lx2, the block dimension variables (w,h),
591
 *  average_flag, and by the very order in which Predict() is called.
592
 *  This implementation design (implicitly different than the spec)
593
 *  was chosen for its elegance.
594
*/
595
 
596
static void form_component_prediction(src,dst,lx,lx2,w,h,x,y,dx,dy,average_flag)
597
unsigned char *src;
598
unsigned char *dst;
599
int lx;          /* raster line increment */
600
int lx2;
601
int w,h;
602
int x,y;
603
int dx,dy;
604
int average_flag;      /* flag that signals bi-directional or Dual-Prime
605
                          averaging (7.6.7.1 and 7.6.7.4). if average_flag==1,
606
                          a previously formed prediction has been stored in
607
                          pel_pred[] */
608
{
609
  int xint;      /* horizontal integer sample vector: analogous to int_vec[0] */
610
  int yint;      /* vertical integer sample vectors: analogous to int_vec[1] */
611
  int xh;        /* horizontal half sample flag: analogous to half_flag[0]  */
612
  int yh;        /* vertical half sample flag: analogous to half_flag[1]  */
613
  int i, j, v;
614
  unsigned char *s;    /* source pointer: analogous to pel_ref[][]   */
615
  unsigned char *d;    /* destination pointer:  analogous to pel_pred[][]  */
616
 
617
  /* half pel scaling for integer vectors */
618
  xint = dx>>1;
619
  yint = dy>>1;
620
 
621
  /* derive half pel flags */
622
  xh = dx & 1;
623
  yh = dy & 1;
624
 
625
  /* compute the linear address of pel_ref[][] and pel_pred[][]
626
     based on cartesian/raster cordinates provided */
627
  s = src + lx*(y+yint) + x + xint;
628
  d = dst + lx*y + x;
629
 
630
#ifdef TRACE_RECON
631
  if (Trace_Flag)
632
  {
633
    printf("form_component_prediction: xint: %i xh: %i yint: %i yh: %i x: %i y: %i s: src+%i d: dst+%i\n", xint, xh, yint, yh, x, y, lx*(y+yint) + x + xint, lx*y + x);
634
  }
635
#endif /* TRACE_RECON */
636
 
637
  if (!xh && !yh) /* no horizontal nor vertical half-pel */
638
  {
639
    if (average_flag)
640
    {
641
      for (j=0; j<h; j++)
642
      {
643
        for (i=0; i<w; i++)
644
        {
645
#ifdef TRACE_RECON
646
          if (Trace_Flag)
647
          {
648
            printf("form_component_prediction (1): 0.5 * ( ");
649
            printPixel(&d[i]);
650
            printf(" + ");
651
            printPixel(&s[i]);
652
            printf(" ) ->  ");
653
          }
654
#endif /* TRACE_RECON */
655
          v = d[i]+s[i];
656
          d[i] = (v+(v>=0?1:0))>>1;
657
#ifdef TRACE_RECON
658
          if (Trace_Flag)
659
          {
660
            printPixel(&d[i]);
661
            printf("\n");
662
          }
663
#endif /* TRACE_RECON */
664
        }
665
 
666
        s+= lx2;
667
        d+= lx2;
668
      }
669
    }
670
    else
671
    {
672
      for (j=0; j<h; j++)
673
      {
674
        for (i=0; i<w; i++)
675
        {
676
#ifdef TRACE_RECON
677
          if (Trace_Flag)
678
          {
679
            printf("form_component_prediction (2):");
680
            printPixel(&s[i]);
681
            printf(" ->  ");
682
          }
683
#endif /* TRACE_RECON */
684
          d[i] = s[i];
685
#ifdef TRACE_RECON
686
          if (Trace_Flag)
687
          {
688
            printPixel(&d[i]);
689
            printf("\n");
690
          }
691
#endif /* TRACE_RECON */
692
        }
693
 
694
        s+= lx2;
695
        d+= lx2;
696
      }
697
    }
698
  }
699
  else if (!xh && yh) /* no horizontal but vertical half-pel */
700
  {
701
    if (average_flag)
702
    {
703
      for (j=0; j<h; j++)
704
      {
705
        for (i=0; i<w; i++)
706
        {
707
#ifdef TRACE_RECON
708
          if (Trace_Flag)
709
          {
710
            printf("form_component_prediction (3): 0.5 * (");
711
            printPixel(&d[i]);
712
            printf(" + 0.5 * (");
713
            printPixel(&s[i]);
714
            printf(" + ");
715
            printPixel(&s[i+lx+1]);
716
            printf(" )) ->  ");
717
          }
718
#endif /* TRACE_RECON */
719
          v = d[i] + ((unsigned int)(s[i]+s[i+lx]+1)>>1);
720
          d[i]=(v+(v>=0?1:0))>>1;
721
#ifdef TRACE_RECON
722
          if (Trace_Flag)
723
          {
724
            printPixel(&d[i]);
725
            printf("\n");
726
          }
727
#endif /* TRACE_RECON */
728
        }
729
 
730
        s+= lx2;
731
        d+= lx2;
732
      }
733
    }
734
    else
735
    {
736
      for (j=0; j<h; j++)
737
      {
738
        for (i=0; i<w; i++)
739
        {
740
#ifdef TRACE_RECON
741
          if (Trace_Flag)
742
          {
743
            printf("form_component_prediction (4): 0.5 * (");
744
            printPixel(&s[i]);
745
            printf(" + ");
746
            printPixel(&s[i+lx]);
747
            printf(") ->  ");
748
          }
749
#endif /* TRACE_RECON */
750
          d[i] = (unsigned int)(s[i]+s[i+lx]+1)>>1;
751
#ifdef TRACE_RECON
752
          if (Trace_Flag)
753
          {
754
            printPixel(&d[i]);
755
            printf("\n");
756
          }
757
#endif /* TRACE_RECON */
758
        }
759
 
760
        s+= lx2;
761
        d+= lx2;
762
      }
763
    }
764
  }
765
  else if (xh && !yh) /* horizontal but no vertical half-pel */
766
  {
767
    if (average_flag)
768
    {
769
      for (j=0; j<h; j++)
770
      {
771
        for (i=0; i<w; i++)
772
        {
773
#ifdef TRACE_RECON
774
          if (Trace_Flag)
775
          {
776
            printf("form_component_prediction (5): 0.5 * (");
777
            printPixel(&d[i]);
778
            printf(" + 0.5 * (");
779
            printPixel(&s[i]);
780
            printf(" + ");
781
            printPixel(&s[i+1]);
782
            printf(")) ->  ");
783
          }
784
#endif /* TRACE_RECON */
785
          v = d[i] + ((unsigned int)(s[i]+s[i+1]+1)>>1);
786
          d[i] = (v+(v>=0?1:0))>>1;
787
#ifdef TRACE_RECON
788
          if (Trace_Flag)
789
          {
790
            printPixel(&d[i]);
791
            printf("\n");
792
          }
793
#endif /* TRACE_RECON */
794
        }
795
 
796
        s+= lx2;
797
        d+= lx2;
798
      }
799
    }
800
    else
801
    {
802
      for (j=0; j<h; j++)
803
      {
804
        for (i=0; i<w; i++)
805
        {
806
#ifdef TRACE_RECON
807
          if (Trace_Flag)
808
          {
809
            printf("form_component_prediction (6): 0.5 * (");
810
            printPixel(&s[i]);
811
            printf(" + ");
812
            printPixel(&s[i+1]);
813
            printf(") ->  ");
814
          }
815
#endif /* TRACE_RECON */
816
          d[i] = (unsigned int)(s[i]+s[i+1]+1)>>1;
817
#ifdef TRACE_RECON
818
          if (Trace_Flag)
819
          {
820
            printPixel(&d[i]);
821
            printf("\n");
822
          }
823
#endif /* TRACE_RECON */
824
        }
825
 
826
        s+= lx2;
827
        d+= lx2;
828
      }
829
    }
830
  }
831
  else /* if (xh && yh) horizontal and vertical half-pel */
832
  {
833
    if (average_flag)
834
    {
835
      for (j=0; j<h; j++)
836
      {
837
        for (i=0; i<w; i++)
838
        {
839
#ifdef TRACE_RECON
840
          if (Trace_Flag)
841
          {
842
            printf("form_component_prediction (7): 0.5 * (");
843
            printPixel(&d[i]);
844
            printf(" + 0.25 * (");
845
            printPixel(&s[i]);
846
            printf(" + ");
847
            printPixel(&s[i+1]);
848
            printf(" + ");
849
            printPixel(&s[i+lx]);
850
            printf(" + ");
851
            printPixel(&s[i+lx+1]);
852
            printf(")) -> ");
853
          }
854
#endif /* TRACE_RECON */
855
          v = d[i] + ((unsigned int)(s[i]+s[i+1]+s[i+lx]+s[i+lx+1]+2)>>2);
856
          d[i] = (v+(v>=0?1:0))>>1;
857
#ifdef TRACE_RECON
858
          if (Trace_Flag)
859
          {
860
            printPixel(&d[i]);
861
            printf("\n");
862
          }
863
#endif /* TRACE_RECON */
864
        }
865
 
866
        s+= lx2;
867
        d+= lx2;
868
      }
869
    }
870
    else
871
    {
872
      for (j=0; j<h; j++)
873
      {
874
        for (i=0; i<w; i++)
875
        {
876
#ifdef TRACE_RECON
877
          if (Trace_Flag)
878
          {
879
            printf("form_component_prediction (8): 0.25 * (");
880
            printPixel(&s[i]);
881
            printf(" + ");
882
            printPixel(&s[i+1]);
883
            printf(" + ");
884
            printPixel(&s[i+lx]);
885
            printf(" + ");
886
            printPixel(&s[i+lx+1]);
887
            printf(") -> ");
888
          }
889
#endif /* TRACE_RECON */
890
          d[i] = (unsigned int)(s[i]+s[i+1]+s[i+lx]+s[i+lx+1]+2)>>2;
891
#ifdef TRACE_RECON
892
          if (Trace_Flag)
893
          {
894
            printPixel(&d[i]);
895
            printf("\n");
896
          }
897
#endif /* TRACE_RECON */
898
        }
899
 
900
        s+= lx2;
901
        d+= lx2;
902
      }
903
    }
904
  }
905
}

powered by: WebSVN 2.1.0

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