FreeWRL / FreeX3D 4.3.0
mpeg_berkley.h
1/*
2 * Copyright (c) 1995 The Regents of the University of California.
3 * All rights reserved.
4 *
5 * Permission to use, copy, modify, and distribute this software and its
6 * documentation for any purpose, without fee, and without written agreement is
7 * hereby granted, provided that the above copyright notice and the following
8 * two paragraphs appear in all copies of this software.
9 *
10 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
11 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
12 * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
13 * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14 *
15 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
16 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
17 * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
18 * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
19 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
20 */
21
22/*
23 * Portions of this software Copyright (c) 1995 Brown University.
24 * All rights reserved.
25 *
26 * Permission to use, copy, modify, and distribute this software and its
27 * documentation for any purpose, without fee, and without written agreement
28 * is hereby granted, provided that the above copyright notice and the
29 * following two paragraphs appear in all copies of this software.
30 *
31 * IN NO EVENT SHALL BROWN UNIVERSITY BE LIABLE TO ANY PARTY FOR
32 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
33 * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF BROWN
34 * UNIVERSITY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 *
36 * BROWN UNIVERSITY SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT
37 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
38 * PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS"
39 * BASIS, AND BROWN UNIVERSITY HAS NO OBLIGATION TO PROVIDE MAINTENANCE,
40 * SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
41 */
42
43#include <stdio.h>
44#define TRUE 1
45#define FALSE 0
46
47
48/* Define Parsing error codes. */
49
50#define SKIP_PICTURE (-10)
51#define SKIP_TO_START_CODE (-1)
52#define PARSE_OK 1
53
54/* Set ring buffer size. */
55
56#define RING_BUF_SIZE 5
57
58/* Macros for picture code type. */
59
60#define I_TYPE 1
61#define P_TYPE 2
62#define B_TYPE 3
63#define D_TYPE 4
64
65/* Start codes. */
66
67#define SEQ_END_CODE 0x000001b7
68#define SEQ_START_CODE 0x000001b3
69#define GOP_START_CODE 0x000001b8
70#define PICTURE_START_CODE 0x00000100
71#define SLICE_MIN_START_CODE 0x00000101
72#define SLICE_MAX_START_CODE 0x000001af
73#define EXT_START_CODE 0x000001b5
74#define USER_START_CODE 0x000001b2
75#define SEQUENCE_ERROR_CODE 0x000001b4
76
77/* Number of macroblocks to process in one call to mpeg_VidRsrc. */
78
79#define MB_QUANTUM 100
80
81/* Macros used with macroblock address decoding. */
82
83#define MB_STUFFING 34
84#define MB_ESCAPE 35
85
86/* Lock flags for pict images. */
87
88#define DISPLAY_LOCK 0x01
89#define PAST_LOCK 0x02
90#define FUTURE_LOCK 0x04
91
92#define MONO_THRESHOLD 11
93
94/* External declaration of row,col to zig zag conversion matrix. */
95
96/* Brown - changed to const int because it is a help variable */
97extern const int scan[][8];
98
99/* Temporary definition of time stamp structure. */
100
101typedef int TimeStamp;
102
103/* Structure with reconstructed pixel values. */
104
105typedef struct pict_image {
106 unsigned char *luminance; /* Luminance plane. */
107 unsigned char *Cr; /* Cr plane. */
108 unsigned char *Cb; /* Cb plane. */
109 unsigned char *display; /* Display plane. */
110 int locked; /* Lock flag. */
111 TimeStamp show_time; /* Presentation time. */
112} PictImage;
113
114/* Group of pictures structure. */
115
116typedef struct GoP {
117 int drop_flag; /* Flag indicating dropped frame. */
118 unsigned int tc_hours; /* Hour component of time code. */
119 unsigned int tc_minutes; /* Minute component of time code. */
120 unsigned int tc_seconds; /* Second component of time code. */
121 unsigned int tc_pictures; /* Picture counter of time code. */
122 int closed_gop; /* Indicates no pred. vectors to
123 previous group of pictures. */
124 int broken_link; /* B frame unable to be decoded. */
125 char *ext_data; /* Extension data. */
126 char *user_data; /* User data. */
127} GoP;
128
129/* Picture structure. */
130
131typedef struct pict {
132 unsigned int temp_ref; /* Temporal reference. */
133 unsigned int code_type; /* Frame type: P, B, I */
134 unsigned int vbv_delay; /* Buffer delay. */
135 int full_pel_forw_vector; /* Forw. vectors specified in full
136 pixel values flag. */
137 unsigned int forw_r_size; /* Used for vector decoding. */
138 unsigned int forw_f; /* Used for vector decoding. */
139 int full_pel_back_vector; /* Back vectors specified in full
140 pixel values flag. */
141 unsigned int back_r_size; /* Used in decoding. */
142 unsigned int back_f; /* Used in decoding. */
143 char *extra_info; /* Extra bit picture info. */
144 char *ext_data; /* Extension data. */
145 char *user_data; /* User data. */
146} Pict;
147
148/* Slice structure. */
149
150typedef struct slice {
151 unsigned int vert_pos; /* Vertical position of slice. */
152 unsigned int quant_scale; /* Quantization scale. */
153 char *extra_info; /* Extra bit slice info. */
154} Slice;
155
156/* Macroblock structure. */
157
158typedef struct macroblock {
159 int mb_address; /* Macroblock address. */
160 int past_mb_addr; /* Previous mblock address. */
161 int motion_h_forw_code; /* Forw. horiz. motion vector code. */
162 unsigned int motion_h_forw_r; /* Used in decoding vectors. */
163 int motion_v_forw_code; /* Forw. vert. motion vector code. */
164 unsigned int motion_v_forw_r; /* Used in decdoinge vectors. */
165 int motion_h_back_code; /* Back horiz. motion vector code. */
166 unsigned int motion_h_back_r; /* Used in decoding vectors. */
167 int motion_v_back_code; /* Back vert. motion vector code. */
168 unsigned int motion_v_back_r; /* Used in decoding vectors. */
169 unsigned int cbp; /* Coded block pattern. */
170 int mb_intra; /* Intracoded mblock flag. */
171 int bpict_past_forw; /* Past B frame forw. vector flag. */
172 int bpict_past_back; /* Past B frame back vector flag. */
173 int past_intra_addr; /* Addr of last intracoded mblock. */
174 int recon_right_for_prev; /* Past right forw. vector. */
175 int recon_down_for_prev; /* Past down forw. vector. */
176 int recon_right_back_prev; /* Past right back vector. */
177 int recon_down_back_prev; /* Past down back vector. */
178} Macroblock;
179
180/* Block structure. */
181
182typedef struct block {
183 short int dct_recon[8][8]; /* Reconstructed dct coeff matrix. */
184 short int dct_dc_y_past; /* Past lum. dc dct coefficient. */
185 short int dct_dc_cr_past; /* Past cr dc dct coefficient. */
186 short int dct_dc_cb_past; /* Past cb dc dct coefficient. */
187} Block;
188
189/* Video stream structure. */
190
191typedef struct vid_stream {
192 unsigned int h_size; /* Horiz. size in pixels. */
193 unsigned int v_size; /* Vert. size in pixels. */
194 unsigned int mb_height; /* Vert. size in mblocks. */
195 unsigned int mb_width; /* Horiz. size in mblocks. */
196 unsigned char aspect_ratio; /* Code for aspect ratio. */
197 unsigned char picture_rate; /* Code for picture rate. */
198 unsigned int bit_rate; /* Bit rate. */
199 unsigned int vbv_buffer_size; /* Minimum buffer size. */
200 int const_param_flag; /* Contrained parameter flag. */
201 unsigned char intra_quant_matrix[8][8]; /* Quantization matrix for
202 intracoded frames. */
203 unsigned char non_intra_quant_matrix[8][8]; /* Quanitization matrix for
204 non intracoded frames. */
205 char *ext_data; /* Extension data. */
206 char *user_data; /* User data. */
207 GoP group; /* Current group of pict. */
208 Pict picture; /* Current picture. */
209 Slice slice; /* Current slice. */
210 Macroblock mblock; /* Current macroblock. */
211 Block block; /* Current block. */
212 int state; /* State of decoding. */
213 int bit_offset; /* Bit offset in stream. */
214 unsigned int *buffer; /* Pointer to next byte in
215 buffer. */
216 int buf_length; /* Length of remaining buffer.*/
217 unsigned int *buf_start; /* Pointer to buffer start. */
218/* Brown - beginning of added variables that used to be static or global */
219 int max_buf_length; /* Max length of buffer. */
220 int film_has_ended; /* Boolean - film has ended */
221 int sys_layer; /* -1 uninitialized,
222 0 video layer,
223 1 syslayer */
224 unsigned int num_left; /* from ReadPacket - leftover */
225 unsigned int leftover_bytes; /* from ReadPacket - leftover */
226 int EOF_flag; /* stream is EOF */
227 FILE *input; /* stream comes from here */
228 long seekValue; /* 0 no seeking
229 >0 do a seek,
230 <0 already has done seek */
231 int swap; /* from ReadFile */
232 int Parse_done; /* from read_sys */
233 int gAudioStreamID;
234 int gVideoStreamID;
235 int gReservedStreamID;
236 int right_for,down_for; /* From ReconPMBlock, video.c */
237 int right_half_for, down_half_for;
238 unsigned int curBits; /* current bits */
239 int matched_depth; /* depth of displayed movie */
240 char *filename; /* Name of stream filename */
241 int ditherType; /* What type of dithering */
242 char *ditherFlags; /* flags for MB Ordered dither*/
243 int totNumFrames; /* Total Number of Frames */
244 double realTimeStart; /* When did the movie start? */
245/* Brown - end of added variables */
246 PictImage *past; /* Past predictive frame. */
247 PictImage *future; /* Future predictive frame. */
248 PictImage *current; /* Current frame. */
249 PictImage *ring[RING_BUF_SIZE]; /* Ring buffer of frames. */
250 /* x,y size of PPM output file */
251 int ppm_width, ppm_height, ppm_modulus;
253
254
255/* Declaration of global display pointer. */
256
257
258
259/* Definition of Contant integer scale factor. */
260
261#define CONST_BITS 13
262
263/* Misc DCT definitions */
264#define DCTSIZE 8 /* The basic DCT block is 8x8 samples */
265#define DCTSIZE2 64 /* DCTSIZE squared; # of elements in a block */
266
267
268typedef short DCTELEM;
269typedef DCTELEM DCTBLOCK[DCTSIZE2];
270
271#ifdef __STDC__
272# define P(s) s
273#include <stdlib.h> /* used by almost all modules */
274#else
275# define P(s) ()
276#endif
277
278/* util.c */
279void correct_underflow P((mpeg_VidStream *vid_stream ));
280int next_bits P((int num , unsigned int mask , mpeg_VidStream *vid_stream ));
281char *get_ext_data P((mpeg_VidStream *vid_stream ));
282int next_start_code P((mpeg_VidStream *vid_stream));
283char *get_extra_bit_info P((mpeg_VidStream *vid_stream ));
284
285/* video.c */
286void init_stats P((void ));
287void PrintAllStats P((mpeg_VidStream *vid_stream ));
288double ReadSysClock P((void ));
289void PrintTimeInfo P(( mpeg_VidStream *vid_stream ));
290void InitCrop P((void ));
291mpeg_VidStream *Newmpeg_VidStream P((unsigned int buffer_len ));
292#ifndef NOCONTROLS
293void Resetmpeg_VidStream P((mpeg_VidStream *vid ));
294#endif
295void Destroympeg_VidStream P((mpeg_VidStream *astream));
296PictImage *NewPictImage P(( mpeg_VidStream *vid_stream ));
297void DestroyPictImage P((PictImage *apictimage));
298mpeg_VidStream *mpeg_VidRsrc P((TimeStamp time_stamp,mpeg_VidStream *vid_stream, int first ));
299void SetBFlag P((int val ));
300void SetPFlag P((int val ));
301
302/* parseblock.c */
303void ParseReconBlock P((int n, mpeg_VidStream *vid_stream ));
304void ParseAwayBlock P((int n , mpeg_VidStream *vid_stream ));
305
306/* motionvector.c */
307void ComputeForwVector P((int *recon_right_for_ptr , int *recon_down_for_ptr , mpeg_VidStream *the_stream ));
308void ComputeBackVector P((int *recon_right_back_ptr , int *recon_down_back_ptr, mpeg_VidStream *the_stream ));
309
310/* decoders.c */
311void mpeg_init_tables P((void ));
312void decodeDCTDCSizeLum P((unsigned int *value ));
313void decodeDCTDCSizeChrom P((unsigned int *value ));
314void decodeDCTCoeffFirst P((unsigned int *run , int *level ));
315void decodeDCTCoeffNext P((unsigned int *run , int *level ));
316
317/* readfile.c */
318void clear_data_stream P(( mpeg_VidStream *vid_stream));
319int get_more_data P(( mpeg_VidStream *vid_stream ));
320int pure_get_more_data P((unsigned int *buf_start , int max_length , int *length_ptr , unsigned int **buf_ptr, mpeg_VidStream *vid_stream ));
321int read_sys P(( mpeg_VidStream *vid_stream, unsigned int start ));
322int ReadStartCode P(( unsigned int *startCode, mpeg_VidStream *vid_stream ));
323
324int ReadPackHeader P((
325 double *systemClockTime,
326 unsigned long *muxRate,
328
329int ReadSystemHeader P(( mpeg_VidStream *vid_stream ));
330
331int find_start_code P(( FILE *input ));
332
333int ReadPacket P(( unsigned char packetID, mpeg_VidStream *vid_stream ));
334
335void ReadTimeStamp P((
336 unsigned char *inputBuffer,
337 unsigned char *hiBit,
338 unsigned long *low4Bytes));
339
340void ReadSTD P((
341 unsigned char *inputBuffer,
342 unsigned char *stdBufferScale,
343 unsigned long *stdBufferSize));
344
345void ReadRate P((
346 unsigned char *inputBuffer,
347 unsigned long *rate));
348
349int MakeFloatClockTime P((
350 unsigned char hiBit,
351 unsigned long low4Bytes,
352 double *floatClockTime));
353
354
355#undef P
356
357/* Status codes for bit stream i/o operations. */
358
359#define NO_VID_STREAM (-1)
360#define STREAM_UNDERFLOW (-2)
361#define OK 1
362
363/* Size increment of extension data buffers. */
364
365#define EXT_BUF_SIZE 1024
366
367/* External declarations for bitstream i/o operations. */
368extern unsigned int bitMask[];
369extern unsigned int nBitMask[];
370extern unsigned int rBitMask[];
371extern unsigned int bitTest[];
372
373/* Macro for updating bit counter if analysis tool is on. */
374#ifdef ANALYSIS
375#define UPDATE_COUNT(numbits) bitCount += numbits
376#else
377#define UPDATE_COUNT(numbits)
378#endif
379
380#ifdef NO_SANITY_CHECKS
381#define get_bits1(result) \
382{ \
383 UPDATE_COUNT(1); \
384 result = ((vid_stream->curBits & 0x80000000) != 0); \
385 vid_stream->curBits <<= 1; \
386 vid_stream->bit_offset++; \
387 \
388 if (vid_stream->bit_offset & 0x20) { \
389 vid_stream->bit_offset = 0; \
390 vid_stream->buffer++; \
391 vid_stream->curBits = *vid_stream->buffer; \
392 vid_stream->buf_length--; \
393 } \
394}
395
396#define get_bits2(result) \
397{ \
398 UPDATE_COUNT(2); \
399 vid_stream->bit_offset += 2; \
400 \
401 if (vid_stream->bit_offset & 0x20) { \
402 vid_stream->bit_offset -= 32; \
403 vid_stream->buffer++; \
404 vid_stream->buf_length--; \
405 if (vid_stream->bit_offset) { \
406 vid_stream->curBits |= \
407 (*vid_stream->buffer >> (2 - vid_stream->bit_offset)); \
408 } \
409 result = ((vid_stream->curBits & 0xc0000000) >> 30); \
410 vid_stream->curBits = *vid_stream->buffer << vid_stream->bit_offset; \
411 } \
412 \
413 result = ((vid_stream->curBits & 0xc0000000) >> 30); \
414 vid_stream->curBits <<= 2; \
415}
416
417#define get_bitsX(num, mask, shift, result) \
418{ \
419 UPDATE_COUNT(num); \
420 vid_stream->bit_offset += num; \
421 \
422 if (vid_stream->bit_offset & 0x20) { \
423 vid_stream->bit_offset -= 32; \
424 vid_stream->buffer++; \
425 vid_stream->buf_length--; \
426 if (vid_stream->bit_offset) { \
427 vid_stream->curBits |= (*vid_stream->buffer >> \
428 (num - vid_stream->bit_offset)); \
429 } \
430 result = ((vid_stream->curBits & mask) >> shift); \
431 vid_stream->curBits = *vid_stream->buffer << vid_stream->bit_offset; \
432 } \
433 else { \
434 result = ((vid_stream->curBits & mask) >> shift); \
435 vid_stream->curBits <<= num; \
436 } \
437}
438#else
439
440#define get_bits1(result) \
441{ \
442 /* Check for underflow. */ \
443 \
444 if (vid_stream->buf_length < 2) { \
445 correct_underflow(vid_stream); \
446 } \
447 UPDATE_COUNT(1); \
448 result = ((vid_stream->curBits & 0x80000000) != 0); \
449 vid_stream->curBits <<= 1; \
450 vid_stream->bit_offset++; \
451 \
452 if (vid_stream->bit_offset & 0x20) { \
453 vid_stream->bit_offset = 0; \
454 vid_stream->buffer++; \
455 vid_stream->curBits = *vid_stream->buffer; \
456 vid_stream->buf_length--; \
457 } \
458}
459
460#define get_bits2(result) \
461{ \
462 /* Check for underflow. */ \
463 \
464 if (vid_stream->buf_length < 2) { \
465 correct_underflow(vid_stream); \
466 } \
467 UPDATE_COUNT(2); \
468 vid_stream->bit_offset += 2; \
469 \
470 if (vid_stream->bit_offset & 0x20) { \
471 vid_stream->bit_offset -= 32; \
472 vid_stream->buffer++; \
473 vid_stream->buf_length--; \
474 if (vid_stream->bit_offset) { \
475 vid_stream->curBits |= (*vid_stream->buffer >> \
476 (2 - vid_stream->bit_offset)); \
477 } \
478 result = ((vid_stream->curBits & 0xc0000000) >> 30); \
479 vid_stream->curBits = *vid_stream->buffer << vid_stream->bit_offset; \
480 } \
481 \
482 result = ((vid_stream->curBits & 0xc0000000) >> 30); \
483 vid_stream->curBits <<= 2; \
484}
485
486#define get_bitsX(num, mask, shift, result) \
487{ \
488 /* Check for underflow. */ \
489 \
490 if (vid_stream->buf_length < 2) { \
491 correct_underflow(vid_stream); \
492 } \
493 UPDATE_COUNT(num); \
494 vid_stream->bit_offset += num; \
495 \
496 if (vid_stream->bit_offset & 0x20) { \
497 vid_stream->bit_offset -= 32; \
498 vid_stream->buffer++; \
499 vid_stream->buf_length--; \
500 if (vid_stream->bit_offset) { \
501 vid_stream->curBits |= (*vid_stream->buffer >> \
502 (num - vid_stream->bit_offset)); \
503 } \
504 result = ((vid_stream->curBits & mask) >> shift); \
505 vid_stream->curBits = *vid_stream->buffer << vid_stream->bit_offset; \
506 } \
507 else { \
508 result = ((vid_stream->curBits & mask) >> shift); \
509 vid_stream->curBits <<= num; \
510 } \
511}
512#endif
513
514#define get_bits3(result) get_bitsX(3, 0xe0000000, 29, result)
515#define get_bits4(result) get_bitsX(4, 0xf0000000, 28, result)
516#define get_bits5(result) get_bitsX(5, 0xf8000000, 27, result)
517#define get_bits6(result) get_bitsX(6, 0xfc000000, 26, result)
518#define get_bits7(result) get_bitsX(7, 0xfe000000, 25, result)
519#define get_bits8(result) get_bitsX(8, 0xff000000, 24, result)
520#define get_bits9(result) get_bitsX(9, 0xff800000, 23, result)
521#define get_bits10(result) get_bitsX(10, 0xffc00000, 22, result)
522#define get_bits11(result) get_bitsX(11, 0xffe00000, 21, result)
523#define get_bits12(result) get_bitsX(12, 0xfff00000, 20, result)
524#define get_bits14(result) get_bitsX(14, 0xfffc0000, 18, result)
525#define get_bits16(result) get_bitsX(16, 0xffff0000, 16, result)
526#define get_bits18(result) get_bitsX(18, 0xffffc000, 14, result)
527#define get_bits32(result) get_bitsX(32, 0xffffffff, 0, result)
528
529#define get_bitsn(num, result) get_bitsX((num), nBitMask[num], (32-(num)), result)
530
531#ifdef NO_SANITY_CHECKS
532#define show_bits32(result) \
533{ \
534 if (vid_stream->bit_offset) { \
535 result = vid_stream->curBits | (*(vid_stream->buffer+1) >> \
536 (32 - vid_stream->bit_offset)); \
537 } \
538 else { \
539 result = vid_stream->curBits; \
540 } \
541}
542
543#define show_bitsX(num, mask, shift, result) \
544{ \
545 int bO; \
546 bO = vid_stream->bit_offset + num; \
547 if (bO > 32) { \
548 bO -= 32; \
549 result = ((vid_stream->curBits & mask) >> shift) | \
550 (*(vid_stream->buffer+1) >> (shift + (num - bO))); \
551 } \
552 else { \
553 result = ((vid_stream->curBits & mask) >> shift); \
554 } \
555}
556
557#else
558#define show_bits32(result) \
559{ \
560 /* Check for underflow. */ \
561 if (vid_stream->buf_length < 2) { \
562 correct_underflow(vid_stream); \
563 } \
564 if (vid_stream->bit_offset) { \
565 result = vid_stream->curBits | (*(vid_stream->buffer+1) >> \
566 (32 - vid_stream->bit_offset)); \
567 } \
568 else { \
569 result = vid_stream->curBits; \
570 } \
571}
572
573#define show_bitsX(num, mask, shift, result) \
574{ \
575 int bO; \
576 \
577 /* Check for underflow. */ \
578 if (vid_stream->buf_length < 2) { \
579 correct_underflow(vid_stream); \
580 } \
581 bO = vid_stream->bit_offset + num; \
582 if (bO > 32) { \
583 bO -= 32; \
584 result = ((vid_stream->curBits & mask) >> shift) | \
585 (*(vid_stream->buffer+1) >> (shift + (num - bO))); \
586 } \
587 else { \
588 result = ((vid_stream->curBits & mask) >> shift); \
589 } \
590}
591#endif
592
593#define show_bits1(result) show_bitsX(1, 0x80000000, 31, result)
594#define show_bits2(result) show_bitsX(2, 0xc0000000, 30, result)
595#define show_bits3(result) show_bitsX(3, 0xe0000000, 29, result)
596#define show_bits4(result) show_bitsX(4, 0xf0000000, 28, result)
597#define show_bits5(result) show_bitsX(5, 0xf8000000, 27, result)
598#define show_bits6(result) show_bitsX(6, 0xfc000000, 26, result)
599#define show_bits7(result) show_bitsX(7, 0xfe000000, 25, result)
600#define show_bits8(result) show_bitsX(8, 0xff000000, 24, result)
601#define show_bits9(result) show_bitsX(9, 0xff800000, 23, result)
602#define show_bits10(result) show_bitsX(10, 0xffc00000, 22, result)
603#define show_bits11(result) show_bitsX(11, 0xffe00000, 21, result)
604#define show_bits12(result) show_bitsX(12, 0xfff00000, 20, result)
605#define show_bits13(result) show_bitsX(13, 0xfff80000, 19, result)
606#define show_bits14(result) show_bitsX(14, 0xfffc0000, 18, result)
607#define show_bits15(result) show_bitsX(15, 0xfffe0000, 17, result)
608#define show_bits16(result) show_bitsX(16, 0xffff0000, 16, result)
609#define show_bits17(result) show_bitsX(17, 0xffff8000, 15, result)
610#define show_bits18(result) show_bitsX(18, 0xffffc000, 14, result)
611#define show_bits19(result) show_bitsX(19, 0xffffe000, 13, result)
612#define show_bits20(result) show_bitsX(20, 0xfffff000, 12, result)
613#define show_bits21(result) show_bitsX(21, 0xfffff800, 11, result)
614#define show_bits22(result) show_bitsX(22, 0xfffffc00, 10, result)
615#define show_bits23(result) show_bitsX(23, 0xfffffe00, 9, result)
616#define show_bits24(result) show_bitsX(24, 0xffffff00, 8, result)
617#define show_bits25(result) show_bitsX(25, 0xffffff80, 7, result)
618#define show_bits26(result) show_bitsX(26, 0xffffffc0, 6, result)
619#define show_bits27(result) show_bitsX(27, 0xffffffe0, 5, result)
620#define show_bits28(result) show_bitsX(28, 0xfffffff0, 4, result)
621#define show_bits29(result) show_bitsX(29, 0xfffffff8, 3, result)
622#define show_bits30(result) show_bitsX(30, 0xfffffffc, 2, result)
623#define show_bits31(result) show_bitsX(31, 0xfffffffe, 1, result)
624
625#define show_bitsn(num,result) show_bitsX((num), (0xffffffff << (32-(num))), (32-(num)), result)
626
627#ifdef NO_SANITY_CHECKS
628#define flush_bits32 \
629{ \
630 UPDATE_COUNT(32); \
631 \
632 vid_stream->buffer++; \
633 vid_stream->buf_length--; \
634 vid_stream->curBits = *vid_stream->buffer << vid_stream->bit_offset;\
635}
636
637
638#define flush_bits(num) \
639{ \
640 vid_stream->bit_offset += num; \
641 \
642 UPDATE_COUNT(num); \
643 \
644 if (vid_stream->bit_offset & 0x20) { \
645 vid_stream->bit_offset -= 32; \
646 vid_stream->buffer++; \
647 vid_stream->buf_length--; \
648 vid_stream->curBits = *vid_stream->buffer << vid_stream->bit_offset;\
649 } \
650 else { \
651 vid_stream->curBits <<= num; \
652 } \
653}
654#else
655#define flush_bits32 \
656{ \
657 if (vid_stream == NULL) { \
658 /* Deal with no vid stream here. */ \
659 } \
660 \
661 if (vid_stream->buf_length < 2) { \
662 correct_underflow(vid_stream); \
663 } \
664 \
665 UPDATE_COUNT(32); \
666 \
667 vid_stream->buffer++; \
668 vid_stream->buf_length--; \
669 vid_stream->curBits = *vid_stream->buffer << vid_stream->bit_offset;\
670}
671
672#define flush_bits(num) \
673{ \
674 if (vid_stream== NULL) { \
675 /* Deal with no vid stream here. */ \
676 } \
677 \
678 if (vid_stream->buf_length < 2) { \
679 correct_underflow(vid_stream); \
680 } \
681 \
682 UPDATE_COUNT(num); \
683 \
684 vid_stream->bit_offset += num; \
685 \
686 if (vid_stream->bit_offset & 0x20) { \
687 vid_stream->buf_length--; \
688 vid_stream->bit_offset -= 32; \
689 vid_stream->buffer++; \
690 vid_stream->curBits = *vid_stream->buffer << vid_stream->bit_offset;\
691 } \
692 else { \
693 vid_stream->curBits <<= num; \
694 } \
695}
696#endif
697
698#define UTIL2
699
700extern int LUM_RANGE;
701extern int CR_RANGE;
702extern int CB_RANGE;
703
704
705#define CB_BASE 1
706#define CR_BASE (CB_BASE*CB_RANGE)
707#define LUM_BASE (CR_BASE*CR_RANGE)
708
709extern unsigned char pixel[256];
710extern unsigned long wpixel[256];
711extern int *lum_values;
712extern int *cr_values;
713extern int *cb_values;
714
715#define Min(x,y) (((x) < (y)) ? (x) : (y))
716#define Max(x,y) (((x) > (y)) ? (x) : (y))
717
718#define GAMMA_CORRECTION(x) ((int)(pow((x) / 255.0, 1.0 / gammaCorrect) * 255.0))
719#define CHROMA_CORRECTION256(x) ((x) >= 128 \
720 ? 128 + Min(127, (int)(((x) - 128.0) * chromaCorrect)) \
721 : 128 - Min(128, (int)((128.0 - (x)) * chromaCorrect)))
722#define CHROMA_CORRECTION128(x) ((x) >= 0 \
723 ? Min(127, (int)(((x) * chromaCorrect))) \
724 : Max(-128, (int)(((x) * chromaCorrect))))
725#define CHROMA_CORRECTION256D(x) ((x) >= 128 \
726 ? 128.0 + Min(127.0, (((x) - 128.0) * chromaCorrect)) \
727 : 128.0 - Min(128.0, (((128.0 - (x)) * chromaCorrect))))
728#define CHROMA_CORRECTION128D(x) ((x) >= 0 \
729 ? Min(127.0, ((x) * chromaCorrect)) \
730 : Max(-128.0, ((x) * chromaCorrect)))
731
732
733
734/* Code for unbound values in decoding tables */
735#define MPGERROR (-1)
736#define DCT_ERROR 63
737
738#define MACRO_BLOCK_STUFFING 34
739#define MACRO_BLOCK_ESCAPE 35
740
741/* Two types of DCT Coefficients */
742#define DCT_COEFF_FIRST 0
743#define DCT_COEFF_NEXT 1
744
745/* Special values for DCT Coefficients */
746#define END_OF_BLOCK 62
747#define ESCAPE 61
748
749/* Structure for an entry in the decoding table of
750 * macroblock_address_increment */
751typedef struct {
752 int value; /* value for macroblock_address_increment */
753 int num_bits; /* length of the Huffman code */
755
756/* Structure for an entry in the decoding table of macroblock_type */
757typedef struct {
758 unsigned int mb_quant; /* macroblock_quant */
759 unsigned int mb_motion_forward; /* macroblock_motion_forward */
760 unsigned int mb_motion_backward; /* macroblock_motion_backward */
761 unsigned int mb_pattern; /* macroblock_pattern */
762 unsigned int mb_intra; /* macroblock_intra */
763 int num_bits; /* length of the Huffman code */
765
766
767/* Structures for an entry in the decoding table of coded_block_pattern */
768typedef struct {
769 unsigned int cbp; /* coded_block_pattern */
770 int num_bits; /* length of the Huffman code */
772
773/* External declaration of coded block pattern table. */
774
775extern coded_block_pattern_entry coded_block_pattern[512];
776
777
778
779/* Structure for an entry in the decoding table of motion vectors */
780typedef struct {
781 int code; /* value for motion_horizontal_forward_code,
782 * motion_vertical_forward_code,
783 * motion_horizontal_backward_code, or
784 * motion_vertical_backward_code.
785 */
786 int num_bits; /* length of the Huffman code */
788
789
790/* Decoding table for motion vectors */
791extern motion_vectors_entry motion_vectors[2048];
792
793
794/* Structure for an entry in the decoding table of dct_dc_size */
795typedef struct {
796 unsigned int value; /* value of dct_dc_size (luminance or chrominance) */
797 int num_bits; /* length of the Huffman code */
799
800/* DCT coeff tables. */
801
802#define RUN_MASK 0xfc00
803#define LEVEL_MASK 0x03f0
804#define NUM_MASK 0x000f
805#define RUN_SHIFT 10
806#define LEVEL_SHIFT 4
807
808#define DecodeDCTDCSizeLum(macro_val) \
809{ \
810 unsigned int index; \
811 \
812 show_bits5(index); \
813 \
814 if (index < 31) { \
815 macro_val = dct_dc_size_luminance[index].value; \
816 flush_bits(dct_dc_size_luminance[index].num_bits); \
817 } \
818 else { \
819 show_bits9(index); \
820 index -= 0x1f0; \
821 macro_val = dct_dc_size_luminance1[index].value; \
822 flush_bits(dct_dc_size_luminance1[index].num_bits); \
823 } \
824}
825
826#define DecodeDCTDCSizeChrom(macro_val) \
827{ \
828 unsigned int index; \
829 \
830 show_bits5(index); \
831 \
832 if (index < 31) { \
833 macro_val = dct_dc_size_chrominance[index].value; \
834 flush_bits(dct_dc_size_chrominance[index].num_bits); \
835 } \
836 else { \
837 show_bits10(index); \
838 index -= 0x3e0; \
839 macro_val = dct_dc_size_chrominance1[index].value; \
840 flush_bits(dct_dc_size_chrominance1[index].num_bits); \
841 } \
842}
843
844#define DecodeDCTCoeff(dct_coeff_tbl, run, level) \
845{ \
846 unsigned int temp, index; \
847 unsigned int value, next32bits, flushed; \
848 \
849 show_bits32(next32bits); \
850 \
851 /* show_bits8(index); */ \
852 index = next32bits >> 24; \
853 \
854 if (index > 3) { \
855 value = dct_coeff_tbl[index]; \
856 run = value >> RUN_SHIFT; \
857 if (run != END_OF_BLOCK) { \
858 /* num_bits = (value & NUM_MASK) + 1; */ \
859 /* flush_bits(num_bits); */ \
860 if (run != ESCAPE) { \
861 /* get_bits1(value); */ \
862 /* if (value) level = -level; */ \
863 flushed = (value & NUM_MASK) + 2; \
864 level = (value & LEVEL_MASK) >> LEVEL_SHIFT; \
865 value = next32bits >> (32-flushed); \
866 value &= 0x1; \
867 if (value) level = -level; \
868 /* next32bits &= ((~0) >> flushed); last op before update */ \
869 } \
870 else { /* run == ESCAPE */ \
871 /* Get the next six into run, and next 8 into temp */ \
872 /* get_bits14(temp); */ \
873 flushed = (value & NUM_MASK) + 1; \
874 temp = next32bits >> (18-flushed); \
875 /* Normally, we'd ad 14 to flushed, but I've saved a few \
876 * instr by moving the add below */ \
877 temp &= 0x3fff; \
878 run = temp >> 8; \
879 temp &= 0xff; \
880 if (temp == 0) { \
881 /* get_bits8(level); */ \
882 level = next32bits >> (10-flushed); \
883 level &= 0xff; \
884 flushed += 22; \
885 assert(level >= 128); \
886 } else if (temp != 128) { \
887 /* Grab sign bit */ \
888 flushed += 14; \
889 level = ((int) (temp << 24)) >> 24; \
890 } else { \
891 /* get_bits8(level); */ \
892 level = next32bits >> (10-flushed); \
893 level &= 0xff; \
894 flushed += 22; \
895 level = level - 256; \
896 assert(level <= -128 && level >= -255); \
897 } \
898 } \
899 /* Update bitstream... */ \
900 flush_bits(flushed); \
901 assert (flushed <= 32); \
902 } \
903 } \
904 else { \
905 switch (index) { \
906 case 2: { \
907 /* show_bits10(index); */ \
908 index = next32bits >> 22; \
909 value = dct_coeff_tbl_2[index & 3]; \
910 break; \
911 } \
912 case 3: { \
913 /* show_bits10(index); */ \
914 index = next32bits >> 22; \
915 value = dct_coeff_tbl_3[index & 3]; \
916 break; \
917 } \
918 case 1: { \
919 /* show_bits12(index); */ \
920 index = next32bits >> 20; \
921 value = dct_coeff_tbl_1[index & 15]; \
922 break; \
923 } \
924 default: { /* index == 0 */ \
925 /* show_bits16(index); */ \
926 index = next32bits >> 16; \
927 value = dct_coeff_tbl_0[index & 255]; \
928 }} \
929 run = value >> RUN_SHIFT; \
930 level = (value & LEVEL_MASK) >> LEVEL_SHIFT; \
931 \
932 /* \
933 * Fold these operations together to make it fast... \
934 */ \
935 /* num_bits = (value & NUM_MASK) + 1; */ \
936 /* flush_bits(num_bits); */ \
937 /* get_bits1(value); */ \
938 /* if (value) level = -level; */ \
939 \
940 flushed = (value & NUM_MASK) + 2; \
941 value = next32bits >> (32-flushed); \
942 value &= 0x1; \
943 if (value) level = -level; \
944 \
945 /* Update bitstream ... */ \
946 flush_bits(flushed); \
947 assert (flushed <= 32); \
948 } \
949}
950
951#define DecodeDCTCoeffFirst(runval, levelval) \
952{ \
953 DecodeDCTCoeff(dct_coeff_first, runval, levelval); \
954}
955
956#define DecodeDCTCoeffNext(runval, levelval) \
957{ \
958 DecodeDCTCoeff(dct_coeff_next, runval, levelval); \
959}
960
961#define DecodeMBAddrInc(val) \
962{ \
963 unsigned int index; \
964 show_bits11(index); \
965 val = mb_addr_inc[index].value; \
966 flush_bits(mb_addr_inc[index].num_bits); \
967}
968#define DecodeMotionVectors(value) \
969{ \
970 unsigned int index; \
971 show_bits11(index); \
972 value = motion_vectors[index].code; \
973 flush_bits(motion_vectors[index].num_bits); \
974}
975#define DecodeMBTypeB(quant, motion_fwd, motion_bwd, pat, intra) \
976{ \
977 unsigned int index; \
978 \
979 show_bits6(index); \
980 \
981 quant = mb_type_B[index].mb_quant; \
982 motion_fwd = mb_type_B[index].mb_motion_forward; \
983 motion_bwd = mb_type_B[index].mb_motion_backward; \
984 pat = mb_type_B[index].mb_pattern; \
985 intra = mb_type_B[index].mb_intra; \
986 flush_bits(mb_type_B[index].num_bits); \
987}
988#define DecodeMBTypeI(quant, motion_fwd, motion_bwd, pat, intra) \
989{ \
990 unsigned int index; \
991 static int quantTbl[4] = {MPGERROR, 1, 0, 0}; \
992 \
993 show_bits2(index); \
994 \
995 motion_fwd = 0; \
996 motion_bwd = 0; \
997 pat = 0; \
998 intra = 1; \
999 quant = quantTbl[index]; \
1000 if (index) { \
1001 flush_bits (1 + quant); \
1002 } \
1003}
1004#define DecodeMBTypeP(quant, motion_fwd, motion_bwd, pat, intra) \
1005{ \
1006 unsigned int index; \
1007 \
1008 show_bits6(index); \
1009 \
1010 quant = mb_type_P[index].mb_quant; \
1011 motion_fwd = mb_type_P[index].mb_motion_forward; \
1012 motion_bwd = mb_type_P[index].mb_motion_backward; \
1013 pat = mb_type_P[index].mb_pattern; \
1014 intra = mb_type_P[index].mb_intra; \
1015 \
1016 flush_bits(mb_type_P[index].num_bits); \
1017}
1018#define DecodeCBP(coded_bp) \
1019{ \
1020 unsigned int index; \
1021 \
1022 show_bits9(index); \
1023 coded_bp = coded_block_pattern[index].cbp; \
1024 flush_bits(coded_block_pattern[index].num_bits); \
1025}
1026
1027
1028void j_rev_dct_sparse (DCTBLOCK data, int pos);
1029void j_rev_dct (DCTBLOCK data);
1030
1031