Subversion Repositories shark

Rev

Rev 2 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 pj 1
/*
2
 * parseblock.c --
3
 *
4
 *      Procedures to read in the values of a block and store them
5
 *      in a place where the player can use them.
6
 *
7
 */
8
 
9
/*
10
 * Copyright (c) 1995 The Regents of the University of California.
11
 * All rights reserved.
12
 *
13
 * Permission to use, copy, modify, and distribute this software and its
14
 * documentation for any purpose, without fee, and without written agreement is
15
 * hereby granted, provided that the above copyright notice and the following
16
 * two paragraphs appear in all copies of this software.
17
 *
18
 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
19
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
20
 * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
21
 * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
22
 *
23
 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
24
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
25
 * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
26
 * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
27
 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
28
 */
29
 
30
/*
31
 * Portions of this software Copyright (c) 1995 Brown University.
32
 * All rights reserved.
33
 *
34
 * Permission to use, copy, modify, and distribute this software and its
35
 * documentation for any purpose, without fee, and without written agreement
36
 * is hereby granted, provided that the above copyright notice and the
37
 * following two paragraphs appear in all copies of this software.
38
 *
39
 * IN NO EVENT SHALL BROWN UNIVERSITY BE LIABLE TO ANY PARTY FOR
40
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
41
 * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF BROWN
42
 * UNIVERSITY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43
 *
44
 * BROWN UNIVERSITY SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT
45
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
46
 * PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS"
47
 * BASIS, AND BROWN UNIVERSITY HAS NO OBLIGATION TO PROVIDE MAINTENANCE,
48
 * SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
49
 */
50
 
51
#define NO_SANITY_CHECKS
52
#include <assert.h>
53
#include "video.h"
54
#include "proto.h"
55
#include "decoders.h"
56
 
57
 
58
/*
59
   Changes to make the code reentrant:
60
      deglobalized: curBits, bitOffset, bitLength, bitBuffer, curVidStream,
61
      zigzag_direct now a const int variable initialized once
62
   -lsh@cs.brown.edu (Loring Holden)
63
 */
64
 
65
/* External declarations. */
66
extern const int zigzag_direct[];
67
#ifdef DCPREC
68
extern int dcprec;
69
#endif
70
 
71
/* Macro for returning 1 if num is positive, -1 if negative, 0 if 0. */
72
 
73
#define Sign(num) ((num > 0) ? 1 : ((num == 0) ? 0 : -1))
74
 
75
 
76
/*
77
 *--------------------------------------------------------------
78
 *
79
 * ParseReconBlock --
80
 *
81
 *    Parse values for block structure from bitstream.
82
 *      n is an indication of the position of the block within
83
 *      the macroblock (i.e. 0-5) and indicates the type of
84
 *      block (i.e. luminance or chrominance). Reconstructs
85
 *      coefficients from values parsed and puts in
86
 *      block.dct_recon array in vid stream structure.
87
 *      sparseFlag is set when the block contains only one
88
 *      coeffictient and is used by the IDCT.
89
 *
90
 * Results:
91
 *    
92
 *
93
 * Side effects:
94
 *      Bit stream irreversibly parsed.
95
 *
96
 *--------------------------------------------------------------
97
 */
98
 
99
#define DCT_recon blockPtr->dct_recon
100
#define DCT_dc_y_past blockPtr->dct_dc_y_past
101
#define DCT_dc_cr_past blockPtr->dct_dc_cr_past
102
#define DCT_dc_cb_past blockPtr->dct_dc_cb_past
103
 
104
#define DECODE_DCT_COEFF_FIRST DecodeDCTCoeffFirst
105
#define DECODE_DCT_COEFF_NEXT DecodeDCTCoeffNext
106
 
107
void
108
ParseReconBlock(n, vid_stream)
109
     int n;
110
     VidStream *vid_stream;
111
{
112
#ifdef RISC
113
  unsigned int temp_curBits;
114
  int temp_bitOffset;
115
  int temp_bufLength;
116
  unsigned int *temp_bitBuffer;
117
#endif
118
 
119
  Block *blockPtr = &vid_stream->block;
120
  int coeffCount=0;
121
 
122
  if (vid_stream->buf_length < 100)
123
    correct_underflow(vid_stream);
124
 
125
#ifdef RISC
126
  temp_curBits = vid_stream->curBits;
127
  temp_bitOffset = vid_stream->buf_offset;
128
  temp_bufLength = vid_stream->buf_length;
129
  temp_bitBuffer = bitBuffer;
130
#endif
131
 
132
  {
133
    /*
134
     * Copy the VidStream fields curBits, bitOffset, and bitBuffer
135
     * into local variables with the same names, so the macros use the
136
     * local variables instead.  This allows register allocation and
137
     * can provide 1-2 fps speedup.  On machines with not so many registers,
138
     * don't do this.
139
     */
140
#ifdef RISC
141
    register unsigned int curBits = temp_curBits;
142
    register int bitOffset = temp_bitOffset;
143
    register int bufLength = temp_bufLength;
144
    register unsigned int *bitBuffer = temp_bitBuffer;
145
#endif
146
 
147
    int diff;
148
    int size, level=0, i, run, pos, coeff;
149
    short int *reconptr;
150
    unsigned char *iqmatrixptr, *niqmatrixptr;
151
    int qscale;
152
 
153
    reconptr = DCT_recon[0];
154
 
155
    /*
156
     * Hand coded version of memset that's a little faster...
157
     * Old call:
158
     *    memset((char *) DCT_recon, 0, 64*sizeof(short int));
159
     */
160
    {
161
      INT32 *p;
162
      p = (INT32 *) reconptr;
163
 
164
      p[0] = p[1] = p[2] = p[3] = p[4] = p[5] = p[6] = p[7] = p[8] = p[9] =
165
      p[10] = p[11] = p[12] = p[13] = p[14] = p[15] = p[16] = p[17] = p[18] =
166
      p[19] = p[20] = p[21] = p[22] = p[23] = p[24] = p[25] = p[26] = p[27] =
167
      p[28] = p[29] = p[30] = p[31] = 0;
168
 
169
    }
170
 
171
    if (vid_stream->mblock.mb_intra) {
172
 
173
      if (n < 4) {
174
 
175
    /*
176
     * Get the luminance bits.  This code has been hand optimized to
177
     * get by the normal bit parsing routines.  We get some speedup
178
     * by grabbing the next 16 bits and parsing things locally.
179
     * Thus, calls are translated as:
180
     *
181
     *    show_bitsX  <-->   next16bits >> (16-X)
182
     *    get_bitsX   <-->   val = next16bits >> (16-flushed-X);
183
     *               flushed += X;
184
     *               next16bits &= bitMask[flushed];
185
     *    flush_bitsX <-->   flushed += X;
186
     *               next16bits &= bitMask[flushed];
187
     *
188
     * I've streamlined the code a lot, so that we don't have to mask
189
     * out the low order bits and a few of the extra adds are removed.
190
     *    bsmith
191
     */
192
    unsigned int next16bits, index, flushed;
193
 
194
        show_bits16(next16bits);
195
        index = next16bits >> (16-5);
196
        if (index < 31) {
197
          size = dct_dc_size_luminance[index].value;
198
          flushed = dct_dc_size_luminance[index].num_bits;
199
        } else {
200
          index = next16bits >> (16-9);
201
          index -= 0x1f0;
202
          size = dct_dc_size_luminance1[index].value;
203
          flushed = dct_dc_size_luminance1[index].num_bits;
204
        }
205
        next16bits &= bitMask[16+flushed];
206
 
207
        if (size != 0) {
208
          flushed += size;
209
          diff = next16bits >> (16-flushed);
210
          if (!(diff & bitTest[32-size])) {
211
            diff = rBitMask[size] | (diff + 1);
212
          }
213
        } else {
214
          diff = 0;
215
        }
216
        flush_bits(flushed);
217
 
218
        if (n == 0) {
219
          coeff = diff << 3;
220
          if (vid_stream->mblock.mb_address -
221
              vid_stream->mblock.past_intra_addr > 1) {
222
            coeff += 1024;
223
          } else {
224
                    coeff += DCT_dc_y_past;
225
          }
226
          DCT_dc_y_past = coeff;
227
        } else {
228
          coeff = DCT_dc_y_past + (diff << 3);
229
          DCT_dc_y_past = coeff;
230
        }
231
 
232
      } else { /* n = 4 or 5 */
233
 
234
    /*
235
     * Get the chrominance bits.  This code has been hand optimized to
236
     * as described above
237
     */
238
 
239
    unsigned int next16bits, index, flushed;
240
 
241
        show_bits16(next16bits);
242
        index = next16bits >> (16-5);
243
        if (index < 31) {
244
          size = dct_dc_size_chrominance[index].value;
245
          flushed = dct_dc_size_chrominance[index].num_bits;
246
        } else {
247
          index = next16bits >> (16-10);
248
          index -= 0x3e0;
249
          size = dct_dc_size_chrominance1[index].value;
250
          flushed = dct_dc_size_chrominance1[index].num_bits;
251
        }
252
        next16bits &= bitMask[16+flushed];
253
 
254
        if (size != 0) {
255
          flushed += size;
256
          diff = next16bits >> (16-flushed);
257
          if (!(diff & bitTest[32-size])) {
258
            diff = rBitMask[size] | (diff + 1);
259
          }
260
        } else {
261
          diff = 0;
262
        }
263
        flush_bits(flushed);
264
 
265
      /* We test 5 first; a result of the mixup of Cr and Cb */
266
        if (n == 5) {
267
          coeff = diff << 3;
268
 
269
          if (vid_stream->mblock.mb_address -
270
              vid_stream->mblock.past_intra_addr > 1) {
271
            coeff += 1024;
272
          } else {
273
            coeff += DCT_dc_cr_past;
274
          }
275
          DCT_dc_cr_past = coeff;
276
        } else {
277
          coeff = diff << 3;
278
          if (vid_stream->mblock.mb_address -
279
              vid_stream->mblock.past_intra_addr > 1) {
280
            coeff += 1024;
281
          } else {
282
            coeff += DCT_dc_cb_past;
283
          }
284
          DCT_dc_cb_past = coeff;
285
        }
286
      }
287
 
288
      *reconptr = coeff;
289
      i = 0;
290
      pos = 0;
291
      coeffCount = (coeff != 0);
292
 
293
      if (vid_stream->picture.code_type != 4) {
294
 
295
        qscale = vid_stream->slice.quant_scale;
296
        iqmatrixptr = vid_stream->intra_quant_matrix[0];
297
 
298
        while(1) {
299
 
300
          DECODE_DCT_COEFF_NEXT(run, level);
301
 
302
          if (run >= END_OF_BLOCK) break;
303
 
304
          i = i + run + 1;
305
          pos = zigzag_direct[i];
306
 
307
          /* quantizes and oddifies each coefficient */
308
          if (level < 0) {
309
            coeff = ((level<<1) * qscale *
310
                     ((int) (iqmatrixptr[pos]))) / 16;
311
            coeff += (1 - (coeff & 1));
312
          } else {
313
            coeff = ((level<<1) * qscale *
314
                     ((int) (*(iqmatrixptr+pos)))) >> 4;
315
            coeff -= (1 - (coeff & 1));
316
          }
317
#ifdef QUANT_CHECK
318
          printf ("coeff: %d\n", coeff);
319
#endif
320
 
321
          reconptr[pos] = coeff;
322
          coeffCount++;
323
 
324
        }
325
 
326
#ifdef QUANT_CHECK
327
                printf ("\n");
328
#endif
329
 
330
#ifdef ANALYSIS 
331
        {
332
          extern unsigned int *mbCoeffPtr;
333
          mbCoeffPtr[pos]++;
334
        }
335
#endif
336
 
337
        flush_bits(2);
338
                goto end;
339
      }
340
    } else { /* non-intra-coded macroblock */
341
 
342
      niqmatrixptr = vid_stream->non_intra_quant_matrix[0];
343
      qscale = vid_stream->slice.quant_scale;
344
 
345
      DECODE_DCT_COEFF_FIRST(run, level);
346
      i = run;
347
 
348
      pos = zigzag_direct[i];
349
 
350
        /* quantizes and oddifies each coefficient */
351
      if (level < 0) {
352
        coeff = (((level<<1) - 1) * qscale *
353
                 ((int) (niqmatrixptr[pos]))) / 16;
354
        if ((coeff & 1) == 0) {coeff = coeff + 1;}
355
      } else {
356
        coeff = (((level<<1) + 1) * qscale *
357
                 ((int) (*(niqmatrixptr+pos)))) >> 4;
358
        coeff = (coeff-1) | 1; /* equivalent to: if ((coeff&1)==0) coeff = coeff - 1; */
359
      }
360
 
361
      reconptr[pos] = coeff;
362
      if (coeff) {
363
          coeffCount = 1;
364
      }
365
 
366
      if (vid_stream->picture.code_type != 4) {
367
 
368
        while(1) {
369
 
370
          DECODE_DCT_COEFF_NEXT(run, level);
371
 
372
          if (run >= END_OF_BLOCK) {
373
                break;
374
          }
375
 
376
          i = i+run+1;
377
          pos = zigzag_direct[i];
378
          if (level < 0) {
379
            coeff = (((level<<1) - 1) * qscale *
380
                     ((int) (niqmatrixptr[pos]))) / 16;
381
            if ((coeff & 1) == 0) {coeff = coeff + 1;}
382
          } else {
383
            coeff = (((level<<1) + 1) * qscale *
384
                     ((int) (*(niqmatrixptr+pos)))) >> 4;
385
            coeff = (coeff-1) | 1; /* equivalent to: if ((coeff&1)==0) coeff = coeff - 1; */
386
          }
387
          reconptr[pos] = coeff;
388
          coeffCount++;
389
        } /* end while */
390
 
391
#ifdef ANALYSIS
392
        {
393
          extern unsigned int *mbCoeffPtr;
394
          mbCoeffPtr[pos]++;
395
        }
396
#endif
397
 
398
        flush_bits(2);
399
 
400
        goto end;
401
      } /* end if (vid_stream->picture.code_type != 4) */
402
    }
403
 
404
  end:
405
 
406
    if (coeffCount == 1) {
407
      j_rev_dct_sparse (reconptr, pos);
408
    }
409
    else {
410
#ifdef FLOATDCT
411
      if (qualityFlag) {
412
        float_idct(reconptr);
413
      } else {
414
#endif
415
        j_rev_dct(reconptr);
416
#ifdef FLOATDCT
417
      }
418
#endif
419
    }
420
 
421
#ifdef RISC
422
    temp_curBits = vid_stream->curBits;
423
    temp_bitOffset = vid_stream->bit_offset;
424
    temp_bufLength = vid_stream->buf_length;
425
    temp_bitBuffer = bitBuffer;
426
#endif
427
 
428
  }
429
 
430
#ifdef RISC
431
  vid_stream->curBits = temp_curBits;
432
  vid_stream->bitOffset = temp_bitOffset;
433
  vid_stream->buf_length = temp_bufLength;
434
  bitBuffer = temp_bitBuffer;
435
#endif
436
}
437
 
438
#undef DCT_recon 
439
#undef DCT_dc_y_past 
440
#undef DCT_dc_cr_past 
441
#undef DCT_dc_cb_past 
442
 
443
 
444
/*
445
 *--------------------------------------------------------------
446
 *
447
 * ParseAwayBlock --
448
 *
449
 *    Parses off block values, throwing them away.
450
 *      Used with grayscale dithering.
451
 *
452
 * Results:
453
 *    None.
454
 *
455
 * Side effects:
456
 *      None.
457
 *
458
 *--------------------------------------------------------------
459
 */
460
 
461
void
462
ParseAwayBlock(n, vid_stream)
463
     int n;
464
     VidStream *vid_stream;
465
{
466
  unsigned int diff;
467
  unsigned int size, run;
468
  int level;
469
 
470
  if (vid_stream->buf_length < 100)
471
    correct_underflow(vid_stream);
472
 
473
  if (vid_stream->mblock.mb_intra) {
474
 
475
    /* If the block is a luminance block... */
476
 
477
    if (n < 4) {
478
 
479
      /* Parse and decode size of first coefficient. */
480
 
481
      DecodeDCTDCSizeLum(size);
482
 
483
      /* Parse first coefficient. */
484
 
485
      if (size != 0) {
486
        get_bitsn(size, diff);
487
      }
488
    }
489
 
490
    /* Otherwise, block is chrominance block... */
491
 
492
    else {
493
 
494
      /* Parse and decode size of first coefficient. */
495
 
496
      DecodeDCTDCSizeChrom(size);
497
 
498
      /* Parse first coefficient. */
499
 
500
      if (size != 0) {
501
        get_bitsn(size, diff);
502
      }
503
    }
504
  }
505
 
506
  /* Otherwise, block is not intracoded... */
507
 
508
  else {
509
 
510
    /* Decode and set first coefficient. */
511
 
512
    DECODE_DCT_COEFF_FIRST(run, level);
513
  }
514
 
515
  /* If picture is not D type (i.e. I, P, or B)... */
516
 
517
  if (vid_stream->picture.code_type != 4) {
518
 
519
    /* While end of macroblock has not been reached... */
520
 
521
    while (1) {
522
 
523
      /* Get the dct_coeff_next */
524
 
525
      DECODE_DCT_COEFF_NEXT(run, level);
526
 
527
      if (run >= END_OF_BLOCK) break;
528
    }
529
 
530
    /* End_of_block */
531
 
532
    flush_bits(2);
533
  }
534
}