summaryrefslogtreecommitdiffstats
path: root/mpeglib/lib/mpegplay/recon.cpp
blob: 6116157af81899dd280dac74d9850a607f23fe77 (plain)
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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
/*
  class for reconstruction
  Copyright (C) 1999  Martin Vogt

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU Library General Public License as published by
  the Free Software Foundation.

  For more information look at the file COPYRIGHT in this package

 */


#include "recon.h"


#define DEBUG_RECON(x)
//#define DEBUG_RECON(x) x



Recon::Recon() {
  copyFunctions=new CopyFunctions();
}


Recon::~Recon() {
  delete copyFunctions;
}


/*
 *--------------------------------------------------------------
 *
 * ReconIMBlock --
 *
 *	Reconstructs intra coded macroblock.
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	None.
 *
 *--------------------------------------------------------------
 */

int Recon::ReconIMBlock(int bnum,int mb_row,int mb_col,int row_size,
			short int* dct_start,PictureArray* pictureArray) {
  int row, col;
  unsigned char *dest;
  unsigned char *picDest;
  int lumLength=(pictureArray->getCurrent())->getLumLength();
  int colorLength=(pictureArray->getCurrent())->getColorLength();
  int endDest=0;



  /* If block is luminance block... */

  if (bnum < 4) {

    /* Calculate row and col values for upper left pixel of block. */

    row = mb_row << 4;
    col = mb_col << 4;
    if (bnum > 1)
      row += 8;
    if (bnum % 2)
      col += 8;

    /* Set dest to luminance plane of current pict image. */

    picDest = (pictureArray->getCurrent())->getLuminancePtr();
    endDest=lumLength;

  }
  /* Otherwise if block is Cr block... */
  /* Cr first because of the earlier mixup */

  else if (bnum == 5) {

    /* Set dest to Cr plane of current pict image. */

    picDest = (pictureArray->getCurrent())->getCrPtr();
    endDest=colorLength;

    /* Establish row size. yuv color is half of whole size*/
    row_size >>=1;

    /* Calculate row,col for upper left pixel of block. */

    row = mb_row << 3;
    col = mb_col << 3;
  }
  /* Otherwise block is Cb block, and ... */

  else {

    /* Set dest to Cb plane of current pict image. */

    picDest = (pictureArray->getCurrent())->getCbPtr();
    endDest=colorLength;

    
    /* Establish row size. yuv color is half of whole size*/

    row_size /=2;

    /* Calculate row,col for upper left pixel value of block. */

    row = mb_row << 3;
    col = mb_col << 3;
  }

  
  /*
   * For each pixel in block, set to cropped reconstructed value from inverse
   * dct.
   */

  dest = picDest+row * row_size + col;
  
  
  if ((dest+7*row_size+7 >= picDest+endDest) || (dest < picDest)) {
    DEBUG_RECON(cout << "urg! last resort caught before sigsev -4"<<endl;)
    return false;
  }
  
  
  copyFunctions->copy8_src1linear_crop(dct_start,dest,row_size);


  return true;
}



/*
 *--------------------------------------------------------------
 *
 * ReconPMBlock --
 *
 *	Reconstructs forward predicted macroblocks.
 *
 * Results:
 *      None.
 *
 * Side effects:
 *      None.
 *
 *--------------------------------------------------------------
 */

int Recon::ReconPMBlock(int bnum,
			int recon_right_for,
			int recon_down_for,
			int zflag,
			int mb_row,int mb_col,
			int row_size,short int* dct_start,
			PictureArray* pictureArray,int codeType) {
  int row, col, rr;
  unsigned char *picDest, *past;
  unsigned char *rindex1, *rindex2, *rindex3, *rindex4;
  unsigned char *index;
  int lumLength=(pictureArray->getCurrent())->getLumLength();
  int colorLength=(pictureArray->getCurrent())->getColorLength();
  int endPast=0;
  int endDest=0;


  int right_for;
  int down_for;
  int right_half_for;
  int down_half_for;



  picDest=0;

  if (bnum < 4) {
    
    
    
    /* Set dest to luminance plane of current pict image. */

    picDest = (pictureArray->getCurrent())->getLuminancePtr();
    endDest=lumLength;

    if (codeType == B_TYPE) {
       past = (pictureArray->getPast())->getLuminancePtr();
    } else {

      /* Set predictive frame to current future frame. */

      past = (pictureArray->getFuture())->getLuminancePtr();
    }
    endPast=lumLength;


    /* Calculate row,col of upper left pixel in block. */

    row = mb_row << 4;
    col = mb_col << 4;
    if (bnum > 1)
      row += 8;
    if (bnum % 2)
      col += 8;

  /* Otherwise, block is NOT luminance block, ... */

  } else {
    
    /* Construct motion vectors. */

    recon_right_for >>= 1;
    recon_down_for >>= 1;
    
    /* Establish row size. yuv color is half of whole size*/

    row_size /= 2;

    
    /* Calculate row,col of upper left pixel in block. */
    
    row = mb_row << 3;
    col = mb_col << 3;
    
    /* If block is Cr block... */
    /* 5 first because order was mixed up in earlier versions */
    
    if (bnum == 5) {
      
      /* Set dest to Cr plane of current pict image. */
      
     picDest = (pictureArray->getCurrent())->getCrPtr();
     
     if (codeType == B_TYPE) {
       
       past = (pictureArray->getPast())->getCrPtr();
     } else {
       past = (pictureArray->getFuture())->getCrPtr();
     }
    }
    /* Otherwise, block is Cb block... */
    
    else {
      
      /* Set dest to Cb plane of current pict image. */
      
      picDest = (pictureArray->getCurrent())->getCbPtr();
      
      if (codeType == B_TYPE) {
	past = (pictureArray->getPast())->getCbPtr();
      } else {
	past = (pictureArray->getFuture())->getCbPtr();
      }
    }
    endPast=colorLength;
    endDest=colorLength;
    
  }

  /* Calculate right_back, down_back motion vectors. */
  right_for = recon_right_for >> 1;
  down_for = recon_down_for >> 1;
  right_half_for = recon_right_for & 0x1;
  down_half_for = recon_down_for & 0x1;

  
  /* For each pixel in block... */

  index = picDest + (row * row_size) + col;
  rindex1 = past + (row + down_for) * row_size + col + right_for;




  if ((rindex1+7*row_size+7 >= past+endPast) || (rindex1 < past)) {
    DEBUG_RECON(cout << "urg! last resort caught before sigsev -1"<<endl;)
    return false;
  }
  if ((index+7*row_size+7 >= picDest+endDest) || (index < picDest)) {
    DEBUG_RECON(cout << "urg! last resort caught before sigsev -2"<<endl;)
    return false;
  }

 
  /*
   * Calculate predictive pixel value based on motion vectors and copy to
   * dest plane.
   */
  
  if ((!down_half_for) && (!right_half_for)) {

    if (!zflag) {
      copyFunctions->copy8_src2linear_crop(rindex1,dct_start,index,row_size);
    } else {
      if (right_for & 0x1) {

	/* No alignment, used byte copy */
	copyFunctions->copy8_byte(rindex1,index,row_size);


      } else if (right_for & 0x2) {
	/* Half-word bit aligned, use 16 bit copy */
	unsigned short *src = (unsigned short *)rindex1;
	unsigned short *dest = (unsigned short *)index;
	row_size >>= 1;
	copyFunctions->copy8_word(src,dest,row_size);

      } else {
	/* Word aligned, use 32 bit copy */
	int *src = (int*) rindex1;
	int *dest = (int*) index;

	row_size >>= 2;


	for (rr = 0; rr < 8; rr++) {
	  dest[0] = src[0];
	  dest[1] = src[1];
	  dest += row_size;
	  src += row_size;
	}
	
      }
    }
  } else {
    rindex2 = rindex1 + right_half_for + (down_half_for * row_size);
    
    /* if one of the two is zero, then quality makes no difference */
    if ((!right_half_for) || (!down_half_for) || (!qualityFlag)) {
      
      if (!zflag) {
	// was +1
	copyFunctions->copy8_div2_src3linear_crop(rindex1,rindex2,dct_start,
						  index,row_size);
      } else { /* zflag */
	// was +1
	copyFunctions->copy8_div2_nocrop(rindex1,rindex2,index,row_size);
      }
    } else { /* qualityFlag on and both vectors are non-zero */
      rindex3 = rindex1 + right_half_for;
      rindex4 = rindex1 + (down_half_for * row_size);
      if (!zflag) {
	copyFunctions->copy8_div4_src5linear_crop(rindex1,rindex2,rindex3,
						  rindex4,dct_start,
						  index,row_size);
      } else { /* zflag */
	copyFunctions->copy8_div4_nocrop(rindex1,rindex2,rindex3,rindex4,
					 index,row_size);
      }
    }
    
  }
  
  return true;
}


/*
 *--------------------------------------------------------------
 *
 * ReconBMBlock --
 *
 *	Reconstructs back predicted macroblocks.
 *
 * Results:
 *      None.
 *
 * Side effects:
 *      None.
 *
 *--------------------------------------------------------------
 */

int Recon::ReconBMBlock(int bnum,
			int recon_right_back,
			int recon_down_back,
			int zflag,
			int mb_row,int mb_col,
			int row_size,short int* dct_start,
			PictureArray* pictureArray) {
  int row, col, rr;
  unsigned char *dest, *future;
  int right_back, down_back, right_half_back, down_half_back;
  unsigned char *rindex1, *rindex2, *rindex3, *rindex4;
  unsigned char *index;
  int lumLength=(pictureArray->getCurrent())->getLumLength();
  int colorLength=(pictureArray->getCurrent())->getColorLength();
  
  int endFuture=0;
  int endDest=0;


  /* If block is luminance block... */

  if (bnum < 4) {


    /* Set dest to luminance plane of current pict image. */

    dest = (pictureArray->getCurrent())->getLuminancePtr();
    endDest=lumLength;
    /*
     * If future frame exists, set future to luminance plane of future frame.
     */

    future = (pictureArray->getFuture())->getLuminancePtr();
    endFuture=lumLength;

    /* Calculate row,col of upper left pixel in block. */

    row = mb_row << 4;
    col = mb_col << 4;
    if (bnum > 1)
      row += 8;
    if (bnum % 2)
      col += 8;


  }
  /* Otherwise, block is NOT luminance block, ... */

  else {

    /* Construct motion vectors. */

    recon_right_back >>= 1;
    recon_down_back >>= 1;

    /* Establish row size. yuv color is half of whole size*/

    row_size >>= 1;

    /* Calculate row,col of upper left pixel in block. */

    row = mb_row << 3;
    col = mb_col << 3;


    /* If block is Cr block... */
    /* They were switched earlier, so 5 is first - eyhung */

    if (bnum == 5) {

      /* Set dest to Cr plane of current pict image. */

      dest = (pictureArray->getCurrent())->getCrPtr();

      /*
       * If future frame exists, set future to Cr plane of future image.
       */

      future = (pictureArray->getFuture())->getCrPtr();
    }
    /* Otherwise, block is Cb block... */

    else {

      /* Set dest to Cb plane of current pict image. */

      dest = (pictureArray->getCurrent())->getCbPtr();

      /*
       * If future frame exists, set future to Cb plane of future frame.
       */

      future = (pictureArray->getFuture())->getCbPtr();
    }
    endDest=colorLength;
    endFuture=colorLength;
  }

  /* Calculate right_back, down_back motion vectors. */
  right_back = recon_right_back >> 1;
  down_back = recon_down_back >> 1;
  right_half_back = recon_right_back & 0x1;
  down_half_back = recon_down_back & 0x1;

  /* For each pixel in block do... */
  
  index = dest + (row * row_size) + col;
  rindex1 = future + (row + down_back) * row_size + col + right_back;

  if ((index+7*row_size+7 >= dest+endDest) || (index < dest)) {
    DEBUG_RECON(cout << "urg! last resort -9"<<endl;)
    return false;
  }
  if ((rindex1+7*row_size+7 >= future+endFuture) || (rindex1 < future)) {
    DEBUG_RECON(cout << "urg! last resort -8"<<endl;)
    return false;
  }

  if ((!right_half_back) && (!down_half_back)) {
    if (!zflag) {
      copyFunctions->copy8_src2linear_crop(rindex1,dct_start,index,row_size);
    } else {
      if (right_back & 0x1) {
	/* No alignment, use byte copy */

	copyFunctions->copy8_byte(rindex1,index,row_size);
	
      } else if (right_back & 0x2) {
	/* Half-word bit aligned, use 16 bit copy */
	unsigned short *src = (unsigned short *)rindex1;
	unsigned short *dest = (unsigned short *)index;
	row_size >>= 1;
	copyFunctions->copy8_word(src,dest,row_size);

      } else {
	/* Word aligned, use 32 bit copy */
	int *src = (int *)rindex1;
	int *dest = (int *)index;
	row_size >>= 2;
	for (rr = 0; rr < 8; rr++) {
	  dest[0] = src[0];
	  dest[1] = src[1];
	  dest += row_size;
	  src += row_size;
	}
      }
    }
  } else {
    rindex2 = rindex1 + right_half_back + (down_half_back * row_size);
    if (!qualityFlag) {
      
      if (!zflag) {
	// was +1
	copyFunctions->copy8_div2_src3linear_crop(rindex1,rindex2,dct_start,
						  index,row_size);
      } else { /* zflag */
	// was +1
	copyFunctions->copy8_div2_nocrop(rindex1,rindex2,index,row_size);
      }
    } else { /* qualityFlag on */
      rindex3 = rindex1 + right_half_back;
      rindex4 = rindex1 + (down_half_back * row_size);
      if (!zflag) {
	copyFunctions->copy8_div4_src5linear_crop(rindex1,rindex2,rindex3,
						  rindex4,dct_start,
						  index,row_size);
      } else { /* zflag */
	copyFunctions->copy8_div4_nocrop(rindex1,rindex2,rindex3,rindex4,
					 index,row_size);
      }
    }
    
  }
  return true;
}


/*
 *--------------------------------------------------------------
 *
 * ReconBiMBlock --
 *
 *	Reconstructs bidirectionally predicted macroblocks.
 *
 * Results:
 *      None.
 *
 * Side effects:
 *      None.
 *
 *--------------------------------------------------------------
 */
int Recon::ReconBiMBlock(int bnum,
			 int recon_right_for,
			 int recon_down_for,
			 int recon_right_back,
			 int recon_down_back,
			 int zflag,int mb_row,
			 int mb_col,int row_size,short int* dct_start,
			 PictureArray* pictureArray) {
  int row, col;
  unsigned char *dest, *past=NULL, *future=NULL;
  int right_for, down_for, right_half_for, down_half_for;
  int right_back, down_back, right_half_back, down_half_back;
  unsigned char *index, *rindex1, *bindex1;
  int forw_row_start, back_row_start, forw_col_start, back_col_start;
  int lumLength=(pictureArray->getCurrent())->getLumLength();
  int colorLength=(pictureArray->getCurrent())->getColorLength();
  int endPast=0;
  int endFuture=0;




  /* If block is luminance block... */

  if (bnum < 4) {


    /* Set dest to luminance plane of current pict image. */

    dest = (pictureArray->getCurrent())->getLuminancePtr();

    /* If past frame exists, set past to luminance plane of past frame. */

    past = (pictureArray->getPast())->getLuminancePtr();
    endPast=lumLength;

    /*
     * If future frame exists, set future to luminance plane of future frame.
     */

    future = (pictureArray->getFuture())->getLuminancePtr();
    endFuture=lumLength;

    /* Calculate row,col of upper left pixel in block. */

    row = (mb_row << 4);
    col = (mb_col << 4);
    if (bnum > 1)
      row += 8;
    if (bnum & 0x01)
      col += 8;



  } else {
    /* Otherwise, block is NOT luminance block, ... */

    /* Construct motion vectors. */
    recon_right_for >>= 1;
    recon_down_for >>= 1;
    recon_right_back >>= 1;
    recon_down_back >>= 1;

    /* Establish row size. yuv color is half of whole size*/

    row_size /= 2;


    /* Calculate row,col of upper left pixel in block. */

    row = (mb_row << 3);
    col = (mb_col << 3);



    
    /* If block is Cr block... */
	/* Switched earlier, so we test Cr first - eyhung */

    if (bnum == 5) {
      /* Set dest to Cr plane of current pict image. */

      dest = (pictureArray->getCurrent())->getCrPtr();

      /* If past frame exists, set past to Cr plane of past image. */

      past = (pictureArray->getPast())->getCrPtr();
      endPast=colorLength;
      /*
       * If future frame exists, set future to Cr plane of future image.
       */

      future = (pictureArray->getFuture())->getCrPtr();
      endFuture=colorLength;
    }
    /* Otherwise, block is Cb block... */

    else {
      /* Set dest to Cb plane of current pict image. */
      dest = (pictureArray->getCurrent())->getCbPtr();

      /* If past frame exists, set past to Cb plane of past frame. */

      past = (pictureArray->getPast())->getCbPtr();
      endPast=colorLength;
      
      /*
       * If future frame exists, set future to Cb plane of future frame.
       */

      future = (pictureArray->getFuture())->getCbPtr();
      endFuture=colorLength;
    }
  }
  /*
   * Calculate right_for, down_for, right_half_for, down_half_for,
   * right_back, down_bakc, right_half_back, and down_half_back, motion
   * vectors.
   */
  
  right_for = recon_right_for >> 1;
  down_for = recon_down_for >> 1;
  right_half_for = recon_right_for & 0x1;
  down_half_for = recon_down_for & 0x1;
  
  right_back = recon_right_back >> 1;
  down_back = recon_down_back >> 1;
  right_half_back = recon_right_back & 0x1;
  down_half_back = recon_down_back & 0x1;

  forw_col_start = col + right_for;
  forw_row_start = row + down_for;
  
  back_col_start = col + right_back;
  back_row_start = row + down_back;

  /* For each pixel in block... */

  index = dest + (row * row_size) + col;

  rindex1 = past + forw_row_start  * row_size + forw_col_start;

  bindex1 = future + back_row_start * row_size + back_col_start;

  if ((rindex1+7*row_size+7 >= past+endPast) || (rindex1 < past))  {
    DEBUG_RECON(cout << "urg! last resort -1"<<endl;)
    return false;
  }
  if ((bindex1+7*row_size+7 >= future+endFuture) || (bindex1 < future)) {
    DEBUG_RECON(cout << "urg! last resort -2"<<endl;)
    return false;
  }



  if (!zflag) {
    copyFunctions->copy8_div2_src3linear_crop(rindex1,bindex1,dct_start,
					      index,row_size);
  } else {
    copyFunctions->copy8_div2_nocrop(rindex1,bindex1,index,row_size);
  }

  return true;
}