Subversion Repositories shark

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 pj 1
/*
2
 * mb_ordered.c
3
 *
4
 *      This file contains C code to implement an macroblock ordered dither.
5
 *
6
 */
7
 
8
/*  
9
 * Copyright (c) 1995 The Regents of the University of California.
10
 * All rights reserved.
11
 *
12
 * Permission to use, copy, modify, and distribute this software and its
13
 * documentation for any purpose, without fee, and without written agreement is
14
 * hereby granted, provided that the above copyright notice and the following
15
 * two paragraphs appear in all copies of this software.
16
 *
17
 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
18
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
19
 * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
20
 * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
21
 *
22
 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
23
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
24
 * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
25
 * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
26
 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
27
 */
28
 
29
/*
30
 * Portions of this software Copyright (c) 1995 Brown University.
31
 * All rights reserved.
32
 *
33
 * Permission to use, copy, modify, and distribute this software and its
34
 * documentation for any purpose, without fee, and without written agreement
35
 * is hereby granted, provided that the above copyright notice and the
36
 * following two paragraphs appear in all copies of this software.
37
 *
38
 * IN NO EVENT SHALL BROWN UNIVERSITY BE LIABLE TO ANY PARTY FOR
39
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
40
 * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF BROWN
41
 * UNIVERSITY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42
 *
43
 * BROWN UNIVERSITY SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT
44
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
45
 * PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS"
46
 * BASIS, AND BROWN UNIVERSITY HAS NO OBLIGATION TO PROVIDE MAINTENANCE,
47
 * SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
48
 */
49
 
50
#include "video.h"
51
#include "proto.h"
52
#include "dither.h"
53
 
54
/*
55
   Changes to make the code reentrant:
56
     ditherFlags now not global, passed to MBOrderedDitherImage
57
 
58
    -lsh@cs.brown.edu (Loring Holden)
59
 */
60
 
61
#define DITH_SIZE 16
62
 
63
 
64
/* Structures used to implement macroblock ordered
65
   dither algorithm.
66
*/
67
 
68
static unsigned char ***ditherPtr[DITH_SIZE];
69
 
70
 
71
/*
72
 *--------------------------------------------------------------
73
 *
74
 *  InitMBOrderedDither--
75
 *
76
 *    Structures intialized for ordered dithering.
77
 *
78
 * Results:
79
 *    None.
80
 *
81
 * Side effects:
82
 *      None.
83
 *
84
 *--------------------------------------------------------------
85
 */
86
 
87
void
88
InitMBOrderedDither()
89
{
90
  unsigned char ****pos_2_cb;
91
  unsigned char ***cb_2_cr;
92
  unsigned char **cr_2_l;
93
  int cb_val, cb_rval, cr_val, cr_rval, l_val, l_rval;
94
  int i, j, pos;
95
  int err_range, threshval;
96
 
97
  pos_2_cb = (unsigned char ****) malloc (DITH_SIZE*sizeof(unsigned char ***));
98
  cb_2_cr = (unsigned char ***) malloc(CB_RANGE*sizeof(unsigned char **));
99
  cr_2_l = (unsigned char **) malloc(CR_RANGE*sizeof(unsigned char *));
100
 
101
  for (pos=0; pos<DITH_SIZE; pos++) {
102
 
103
    pos_2_cb[pos] = (unsigned char ***) malloc(256*(sizeof(unsigned char **)));
104
 
105
    for (j=0; j<CB_RANGE; j++) {
106
      cb_2_cr[j] = (unsigned char **) malloc(256*(sizeof(unsigned char *)));
107
    }
108
 
109
    for (cb_val=0; cb_val<cb_values[0]; cb_val++) {
110
      (pos_2_cb[pos])[cb_val] = cb_2_cr[0];
111
    }
112
 
113
    for (cb_rval=0; cb_rval<(CB_RANGE-1); cb_rval++) {
114
      err_range = cb_values[cb_rval+1] - cb_values[cb_rval];
115
      threshval = ((pos*err_range)/DITH_SIZE)+cb_values[cb_rval];
116
 
117
      for (cb_val=cb_values[cb_rval]; cb_val<cb_values[cb_rval+1]; cb_val++) {
118
    if (cb_val>threshval) (pos_2_cb[pos])[cb_val] = cb_2_cr[cb_rval+1];
119
    else (pos_2_cb[pos])[cb_val] = cb_2_cr[cb_rval];
120
      }
121
    }
122
 
123
    for (cb_val=cb_values[CB_RANGE-1]; cb_val<256; cb_val++) {
124
      (pos_2_cb[pos])[cb_val] = cb_2_cr[CB_RANGE-1];
125
    }
126
 
127
    for (cb_rval=0; cb_rval<CB_RANGE; cb_rval++) {
128
 
129
      for (j=0; j<CR_RANGE; j++) {
130
    cr_2_l[j] = (unsigned char *) malloc(256*(sizeof(unsigned char)));
131
      }
132
 
133
      for (cr_val=0; cr_val < cr_values[0]; cr_val++) {
134
    (cb_2_cr[cb_rval])[cr_val] = cr_2_l[0];
135
      }
136
 
137
      for (cr_rval=0; cr_rval<(CR_RANGE-1); cr_rval++) {
138
    err_range = cr_values[cr_rval+1] - cr_values[cr_rval];
139
    threshval = ((pos*err_range)/DITH_SIZE)+cr_values[cr_rval];
140
 
141
    for (cr_val=cr_values[cr_rval]; cr_val<cr_values[cr_rval+1]; cr_val++) {
142
      if (cr_val>threshval) (cb_2_cr[cb_rval])[cr_val] = cr_2_l[cr_rval+1];
143
      else (cb_2_cr[cb_rval])[cr_val] = cr_2_l[cr_rval];
144
    }
145
      }
146
 
147
      for (cr_val=cr_values[CR_RANGE-1]; cr_val<256; cr_val++) {
148
    (cb_2_cr[cb_rval])[cr_val] = cr_2_l[CR_RANGE-1];
149
      }
150
 
151
      for (cr_rval=0; cr_rval<CR_RANGE; cr_rval++) {
152
 
153
    for (l_val = 0; l_val < lum_values[0]; l_val++) {
154
      (cr_2_l[cr_rval])[l_val] = pixel[cb_rval+(cr_rval*CB_RANGE)+
155
                        (0*CR_RANGE*CB_RANGE)];
156
    }
157
 
158
    for (l_rval=0; l_rval<(LUM_RANGE-1); l_rval++) {
159
      err_range = lum_values[l_rval+1] - lum_values[l_rval];
160
      threshval = ((pos*err_range) /DITH_SIZE) + lum_values[l_rval];
161
 
162
      for (l_val = lum_values[l_rval]; l_val < lum_values[l_rval+1]; l_val++) {
163
        if (l_val>threshval) (cr_2_l[cr_rval])[l_val] =
164
          pixel[cb_rval+(cr_rval*CB_RANGE)+((l_rval+1)*CR_RANGE*CB_RANGE)];
165
        else (cr_2_l[cr_rval])[l_val] =
166
          pixel[cb_rval+(cr_rval*CB_RANGE)+(l_rval*CR_RANGE*CB_RANGE)];
167
      }
168
    }
169
 
170
    for (l_val = lum_values[LUM_RANGE-1]; l_val < 256; l_val++) {
171
      (cr_2_l[cr_rval])[l_val] =
172
        pixel[cb_rval+(cr_rval*CB_RANGE)+((LUM_RANGE-1)*CR_RANGE*CB_RANGE)];
173
    }
174
      }
175
    }
176
  }
177
 
178
  for (i=0; i<DITH_SIZE; i++) {
179
    ditherPtr[i] = pos_2_cb[i];
180
  }
181
}
182
 
183
 
184
 
185
/*
186
 *--------------------------------------------------------------
187
 *
188
 * MBOrderedDitherImage --
189
 *
190
 *    Dithers an image using an ordered dither at macroblock level.
191
 *    Assumptions made:
192
 *      1) The color space is allocated y:cr:cb = 8:4:4
193
 *      2) The spatial resolution of y:cr:cb is 4:1:1
194
 *      The channels are dithered based on the standard
195
 *      ordered dither pattern for a 4x4 area.
196
 *
197
 * Results:
198
 *    None.
199
 *
200
 * Side effects:
201
 *    None.
202
 *
203
 *--------------------------------------------------------------
204
 */
205
void
206
MBOrderedDitherImage (lum, cr, cb, out, h, w, ditherFlags)
207
    unsigned char *lum;
208
    unsigned char *cr;
209
    unsigned char *cb;
210
    unsigned char *out;
211
    int h, w;
212
    char *ditherFlags;
213
{
214
  unsigned char *l, *r, *b, *o1, *o2;
215
  unsigned char *l2;
216
  unsigned char L, R, B;
217
  int i, j, mbaddr, mbwidth;
218
  unsigned char ***dp0 = ditherPtr[0];
219
  unsigned char ***dp2 = ditherPtr[2];
220
  unsigned char ***dp4 = ditherPtr[4];
221
  unsigned char ***dp6 = ditherPtr[6];
222
  unsigned char ***dp8 = ditherPtr[8];
223
  unsigned char ***dp10 = ditherPtr[10];
224
  unsigned char ***dp12 = ditherPtr[12];
225
  unsigned char ***dp14 = ditherPtr[14];
226
  unsigned char ***dp1 = ditherPtr[1];
227
  unsigned char ***dp3 = ditherPtr[3];
228
  unsigned char ***dp5 = ditherPtr[5];
229
  unsigned char ***dp7 = ditherPtr[7];
230
  unsigned char ***dp9 = ditherPtr[9];
231
  unsigned char ***dp11 = ditherPtr[11];
232
  unsigned char ***dp13 = ditherPtr[13];
233
  unsigned char ***dp15 = ditherPtr[15];
234
 
235
  l = lum;
236
  l2 = lum + w;
237
  r = cr;
238
  b = cb;
239
  o1 = out;
240
  o2 = out+w;
241
  mbwidth = w / 16;
242
 
243
  for (i=0; i<h; i+=4) {
244
 
245
    mbaddr = (i / 16) * mbwidth ;
246
 
247
    for (j=0; j<w; j+=8) {
248
 
249
      if (ditherFlags[mbaddr+(j/16)]) {
250
    R = r[0]; B = b[0];
251
 
252
    L = l[0];
253
    o1[0] = ((dp0[B])[R])[L];
254
    L = l[1];
255
    o1[1] = ((dp8[B])[R])[L];
256
    L = l2[0];
257
    o2[0] = ((dp12[B])[R])[L];
258
    L = l2[1];
259
    o2[1] = ((dp4[B])[R])[L];
260
 
261
    R = r[1]; B = b[1];
262
 
263
    L = l[2];
264
    o1[2] = ((dp2[B])[R])[L];
265
    L = l[3];
266
    o1[3] = ((dp10[B])[R])[L];
267
    L = l2[2];
268
    o2[2] = ((dp14[B])[R])[L];
269
    L = l2[3];
270
    o2[3] = ((dp6[B])[R])[L];
271
 
272
    R = r[2]; B = b[2];
273
 
274
    L = l[4];
275
    o1[4] = ((dp0[B])[R])[L];
276
    L = l[5];
277
    o1[5] = ((dp8[B])[R])[L];
278
    L = l2[4];
279
    o2[4] = ((dp12[B])[R])[L];
280
    L = l2[5];
281
    o2[5] = ((dp4[B])[R])[L];
282
 
283
    R = r[3]; B = b[3];
284
 
285
    L = l[6];
286
    o1[6] = ((dp2[B])[R])[L];
287
    L = l[7];
288
    o1[7] = ((dp10[B])[R])[L];
289
    L = l2[6];
290
    o2[6] = ((dp14[B])[R])[L];
291
    L = l2[7];
292
    o2[7] = ((dp6[B])[R])[L];
293
      }
294
 
295
      l += 8;
296
      l2 += 8;
297
      r += 4;
298
      b += 4;
299
      o1 += 8;
300
      o2 += 8;
301
    }
302
 
303
    l += w; l2 += w;
304
    o1 += w; o2 += w;
305
 
306
    for (j=0; j<w; j+=8) {
307
 
308
      if (ditherFlags[mbaddr+(j/16)]) {
309
 
310
    R = r[0]; B = b[0];
311
 
312
    L = l[0];
313
    o1[0] = ((dp3[B])[R])[L];
314
    L = l[1];
315
    o1[1] = ((dp11[B])[R])[L];
316
    L = l2[0];
317
    o2[0] = ((dp15[B])[R])[L];
318
    L = l2[1];
319
    o2[1] = ((dp7[B])[R])[L];
320
 
321
    R = r[1]; B = b[1];
322
 
323
    L = l[2];
324
    o1[2] = ((dp1[B])[R])[L];
325
    L = l[3];
326
    o1[3] = ((dp9[B])[R])[L];
327
    L = l2[2];
328
    o2[2] = ((dp13[B])[R])[L];
329
    L = l2[3];
330
    o2[3] = ((dp5[B])[R])[L];
331
 
332
    R = r[2]; B = b[2];
333
 
334
    L = l[4];
335
    o1[4] = ((dp3[B])[R])[L];
336
    L = l[5];
337
    o1[5] = ((dp11[B])[R])[L];
338
    L = l2[4];
339
    o2[4] = ((dp15[B])[R])[L];
340
    L = l2[5];
341
    o2[5] = ((dp7[B])[R])[L];
342
 
343
    R = r[3]; B = b[3];
344
 
345
    L = l[6];
346
    o1[6] = ((dp1[B])[R])[L];
347
    L = l[7];
348
    o1[7] = ((dp9[B])[R])[L];
349
    L = l2[6];
350
    o2[6] = ((dp13[B])[R])[L];
351
    L = l2[7];
352
    o2[7] = ((dp5[B])[R])[L];
353
      }
354
 
355
      l += 8;
356
      l2 += 8;
357
      r += 4;
358
      b += 4;
359
      o1 += 8;
360
      o2 += 8;
361
    }
362
 
363
    l += w; l2 += w;
364
    o1 += w; o2 += w;
365
  }
366
}
367
 
368
 
369
 
370
/*
371
 *--------------------------------------------------------------
372
 *
373
 * MBOrderedDitherDisplayCopy --
374
 *
375
 *    Copies an image to the display (?)
376
 *
377
 * Results:
378
 *    None.
379
 *
380
 * Side effects:
381
 *    None.
382
 *
383
 *--------------------------------------------------------------
384
 */
385
void
386
MBOrderedDitherDisplayCopy(vid_stream, mb_addr, motion_forw, r_right_forw,
387
        r_down_forw, motion_back, r_right_back, r_down_back, past, future)
388
    VidStream *vid_stream;
389
    int mb_addr;
390
    int motion_forw, r_right_forw, r_down_forw;
391
    int motion_back, r_right_back, r_down_back;
392
    unsigned char *past, *future;
393
{
394
  int right_back = 0, right_forw = 0, down_back, down_forw;
395
  unsigned char *dest;
396
  unsigned char *src1 = 0, *src2 = 0;
397
  int row, col, row_size, rr;
398
  int mc, mr;
399
 
400
  row = (mb_addr / vid_stream->mb_width) << 4;
401
  col = (mb_addr % vid_stream->mb_width) << 4;
402
  row_size = vid_stream->mb_width << 4;
403
 
404
  dest = vid_stream->current->display + (row * row_size) + col;
405
 
406
  if (motion_forw) {
407
    right_forw = r_right_forw >> 1;
408
    down_forw = r_down_forw >> 1;
409
    src1 = past + ((row + down_forw) * row_size) + (col + right_forw);
410
  }
411
 
412
  if (motion_back) {
413
    right_back = r_right_back >> 1;
414
    down_back = r_down_back >> 1;
415
    src2 = future + ((row + down_back) * row_size) + (col + right_back);
416
  }
417
 
418
  if (motion_forw) {
419
    if (motion_back) {
420
      for (rr = 0; rr<16; rr++) {
421
        dest[0] = src1[0];      dest[1] = src2[1];
422
        dest[2] = src1[2];      dest[3] = src2[3];
423
        dest[4] = src1[4];      dest[5] = src2[5];
424
        dest[6] = src1[6];      dest[7] = src2[7];
425
        dest[8] = src1[8];      dest[9] = src2[9];
426
        dest[10] = src1[10];    dest[11] = src2[11];
427
        dest[12] = src1[12];    dest[13] = src2[13];
428
        dest[14] = src1[14];    dest[15] = src2[15];
429
 
430
        dest += row_size;
431
        src1 += row_size;
432
        src2 += row_size;
433
      }
434
    }
435
    else {
436
      mc = col & 0x3;
437
      mr = right_forw & 0x3;
438
      if (!mc && !mr) {
439
    /* Use 32 bit copy */
440
    int *d, *s;
441
 
442
    d = (int *) dest;
443
    s = (int *) src1;
444
    row_size /= 4;
445
 
446
    for (rr = 0; rr < 16; rr++) {
447
      d[0] = s[0];
448
      d[1] = s[1];
449
      d[2] = s[2];
450
      d[3] = s[3];
451
      d += row_size;
452
      s += row_size;
453
    }
454
      } else if ((!mc || (mc == 2)) &&
455
         (!mr || (mr == 2))) {
456
    /* Use 16 bit copy */
457
    short int *d, * s;
458
 
459
    d = (short int *) dest;
460
    s = (short int *) src1;
461
    row_size /= 2;
462
 
463
    for (rr = 0; rr < 16; rr++) {
464
      d[0] = s[0];
465
      d[1] = s[1];
466
      d[2] = s[2];
467
      d[3] = s[3];
468
      d[4] = s[4];
469
      d[5] = s[5];
470
      d[6] = s[6];
471
      d[7] = s[7];
472
      d += row_size;
473
      s += row_size;
474
    }
475
      }
476
      else {
477
    for (rr = 0; rr < 16; rr++) {
478
      dest[0] = src1[0];
479
      dest[1] = src1[1];
480
      dest[2] = src1[2];
481
      dest[3] = src1[3];
482
      dest[4] = src1[4];
483
      dest[5] = src1[5];
484
      dest[6] = src1[6];
485
      dest[7] = src1[7];
486
      dest[8] = src1[8];
487
      dest[9] = src1[9];
488
      dest[10] = src1[10];
489
      dest[11] = src1[11];
490
      dest[12] = src1[12];
491
      dest[13] = src1[13];
492
      dest[14] = src1[14];
493
      dest[15] = src1[15];
494
 
495
      dest += row_size;
496
      src1 += row_size;
497
    }
498
      }
499
    }
500
  } else if (motion_back) {
501
    mc = col & 0x3;
502
    mr = right_back & 0x3;
503
    if (!mc && !mr) {
504
      /* Use 32 bit copy */
505
      int *d, *s;
506
 
507
      d = (int *) dest;
508
      s = (int *) src2;
509
      row_size /= 4;
510
 
511
      for (rr = 0; rr < 16; rr++) {
512
        d[0] = s[0];
513
        d[1] = s[1];
514
        d[2] = s[2];
515
        d[3] = s[3];
516
        d += row_size;
517
        s += row_size;
518
      }
519
    }
520
    else if ((!mc || mc == 2) &&
521
         (!mr || mr == 2)) {
522
      /* Use 8 bit copy */
523
      short int *d, *s;
524
 
525
      d = (short int *) dest;
526
      s = (short int *) src2;
527
      row_size /= 2;
528
 
529
      for (rr = 0; rr < 16; rr++) {
530
        d[0] = s[0];
531
        d[1] = s[1];
532
        d[2] = s[2];
533
        d[3] = s[3];
534
        d[4] = s[4];
535
        d[5] = s[5];
536
        d[6] = s[6];
537
        d[7] = s[7];
538
        d += row_size;
539
        s += row_size;
540
      }
541
    }
542
    else {
543
      for (rr = 0; rr < 16; rr++) {
544
        /* Use 8 bit copy */
545
        dest[0] = src2[0];
546
        dest[1] = src2[1];
547
        dest[2] = src2[2];
548
        dest[3] = src2[3];
549
        dest[4] = src2[4];
550
        dest[5] = src2[5];
551
        dest[6] = src2[6];
552
        dest[7] = src2[7];
553
        dest[8] = src2[8];
554
        dest[9] = src2[9];
555
        dest[10] = src2[10];
556
        dest[11] = src2[11];
557
        dest[12] = src2[12];
558
        dest[13] = src2[13];
559
        dest[14] = src2[14];
560
        dest[15] = src2[15];
561
 
562
        dest += row_size;
563
        src2 += row_size;
564
      }
565
    }
566
  }
567
}