FreeWRL / FreeX3D 4.3.0
MPEG_Utils_berkley.c
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#ifndef AQUA
43#include <stdlib.h>
44#include <math.h>
45#include <stdio.h>
46#endif
47#include <assert.h>
48
49//#ifdef AQUA
50//#include <gl.h>
51//#include <glu.h>
52//#include <glext.h>
53//#else
54//#include <GL/gl.h>
55//#include <GL/glu.h>
56//#include <GL/glx.h>
57//#endif
58
59#include "mpeg_berkley.h"
60
61//#include <sys/time.h>
62
63#ifdef __STDC__
64#include <stdlib.h>
65#include <string.h>
66#endif
67
68//#ifdef _MSC_VER
69//#include <Winsock2.h>
70//#else
71//#include <netinet/in.h>
72//#endif
73// hand-coded network byte order functions - UNTESTED AS OF NOV 14, 2016
74int isMachineLittleEndian(); //see LoadTextures.c
75unsigned long int fw_ntohl(unsigned long int networklong){
76 unsigned long int hostlong = 0;
77
78 hostlong |= (networklong & 0xFF000000) >> 24;
79 hostlong |= (networklong & 0x00FF0000) >> 8;
80 hostlong |= (networklong & 0x0000FF00) << 8;
81 hostlong |= (networklong & 0x000000FF) << 24;
82 //if(hostlong != ntohl(networklong)) printf("fw_ntohl is wrong\n");
83 return hostlong;
84}
85unsigned short int fw_ntohs(unsigned short int networkshort){
86 unsigned short int hostshort = 0;
87
88 hostshort |= (networkshort & 0xFF00) >> 8;
89 hostshort |= (networkshort & 0x00FF) << 8;
90 //if(hostshort != ntohs(networkshort)) printf("fw_ntohs is wrong\n");
91 return hostshort;
92}
93unsigned short int fw_htons(unsigned short hostshort)
94{
95 unsigned short int networkshort = 0;
96 if(isMachineLittleEndian()){
97 networkshort |= (hostshort & 0x00ff) << 8;
98 networkshort |= (hostshort & 0xff00) >> 8;
99 }else{
100 networkshort = hostshort;
101 }
102 //if(networkshort != htonl(hostshort)) printf("fw_htons is wrong \n");
103 return networkshort;
104}
105unsigned long int fw_htonl(unsigned long hostlong)
106{
107 unsigned long int networklong = 0;
108 if(isMachineLittleEndian()){
109 if(1){
110 unsigned char *s = (unsigned char *)&hostlong;
111 networklong = ((unsigned long)s[0]) << 24 | ((unsigned long)s[1]) << 16 | ((unsigned long)s[2]) << 8 | s[3];
112 }else{
113 networklong |= (hostlong & 0x000000ff) << 24;
114 networklong |= (hostlong & 0x0000ff00) << 8;
115 networklong |= (hostlong & 0x00ff0000) >> 8;
116 networklong |= (hostlong & 0xff000000) >> 24;
117 }
118 }else{
119 //no-op on bigendian machines
120 networklong = hostlong;
121 }
122 //if(networklong != htonl(hostlong)) printf("fw_htonl is wrong \n");
123 return networklong;
124}
125
126#define NO_SANITY_CHECKS
127extern const int zigzag_direct[];
128
129/* global static return values */
130int *frameCount;
131int *ySize;
132int *xSize;
133int *pictureDepth;
134char *dataPointer;
135
136/* Decoding table for macroblock_address_increment */
137mb_addr_inc_entry mb_addr_inc[2048];
138
139/* Decoding table for macroblock_type in predictive-coded pictures */
140mb_type_entry mb_type_P[64];
141
142/* Decoding table for macroblock_type in bidirectionally-coded pictures */
143mb_type_entry mb_type_B[64];
144
145/* Decoding table for motion vectors */
146motion_vectors_entry motion_vectors[2048];
147
148/* Decoding table for coded_block_pattern */
149
150coded_block_pattern_entry coded_block_pattern[512] =
151{ {(unsigned int)MPGERROR, 0}, {(unsigned int)MPGERROR, 0}, {39, 9}, {27, 9}, {59, 9}, {55, 9}, {47, 9}, {31, 9},
152 {58, 8}, {58, 8}, {54, 8}, {54, 8}, {46, 8}, {46, 8}, {30, 8}, {30, 8},
153 {57, 8}, {57, 8}, {53, 8}, {53, 8}, {45, 8}, {45, 8}, {29, 8}, {29, 8},
154 {38, 8}, {38, 8}, {26, 8}, {26, 8}, {37, 8}, {37, 8}, {25, 8}, {25, 8},
155 {43, 8}, {43, 8}, {23, 8}, {23, 8}, {51, 8}, {51, 8}, {15, 8}, {15, 8},
156 {42, 8}, {42, 8}, {22, 8}, {22, 8}, {50, 8}, {50, 8}, {14, 8}, {14, 8},
157 {41, 8}, {41, 8}, {21, 8}, {21, 8}, {49, 8}, {49, 8}, {13, 8}, {13, 8},
158 {35, 8}, {35, 8}, {19, 8}, {19, 8}, {11, 8}, {11, 8}, {7, 8}, {7, 8},
159 {34, 7}, {34, 7}, {34, 7}, {34, 7}, {18, 7}, {18, 7}, {18, 7}, {18, 7},
160 {10, 7}, {10, 7}, {10, 7}, {10, 7}, {6, 7}, {6, 7}, {6, 7}, {6, 7},
161 {33, 7}, {33, 7}, {33, 7}, {33, 7}, {17, 7}, {17, 7}, {17, 7}, {17, 7},
162 {9, 7}, {9, 7}, {9, 7}, {9, 7}, {5, 7}, {5, 7}, {5, 7}, {5, 7},
163 {63, 6}, {63, 6}, {63, 6}, {63, 6}, {63, 6}, {63, 6}, {63, 6}, {63, 6},
164 {3, 6}, {3, 6}, {3, 6}, {3, 6}, {3, 6}, {3, 6}, {3, 6}, {3, 6},
165 {36, 6}, {36, 6}, {36, 6}, {36, 6}, {36, 6}, {36, 6}, {36, 6}, {36, 6},
166 {24, 6}, {24, 6}, {24, 6}, {24, 6}, {24, 6}, {24, 6}, {24, 6}, {24, 6},
167 {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5},
168 {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5},
169 {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5},
170 {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5},
171 {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5},
172 {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5},
173 {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5},
174 {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5},
175 {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5},
176 {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5},
177 {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5},
178 {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5},
179 {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5},
180 {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5},
181 {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5},
182 {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5},
183 {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5},
184 {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5},
185 {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5},
186 {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5},
187 {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5},
188 {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5},
189 {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5},
190 {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5},
191 {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4},
192 {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4},
193 {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4},
194 {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4},
195 {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4},
196 {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4},
197 {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4},
198 {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4},
199 {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4},
200 {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4},
201 {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4},
202 {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4},
203 {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4},
204 {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4},
205 {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4},
206 {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4},
207 {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3},
208 {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3},
209 {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3},
210 {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3},
211 {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3},
212 {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3},
213 {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3},
214 {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}
215};
216
217/* Decoding tables for dct_dc_size_luminance */
218dct_dc_size_entry dct_dc_size_luminance[32] =
219{ {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2},
220 {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2},
221 {0, 3}, {0, 3}, {0, 3}, {0, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3},
222 {4, 3}, {4, 3}, {4, 3}, {4, 3}, {5, 4}, {5, 4}, {6, 5}, {(unsigned int)MPGERROR, 0}
223};
224
225dct_dc_size_entry dct_dc_size_luminance1[16] =
226{ {7, 6}, {7, 6}, {7, 6}, {7, 6}, {7, 6}, {7, 6}, {7, 6}, {7, 6},
227 {8, 7}, {8, 7}, {8, 7}, {8, 7}, {9, 8}, {9, 8}, {10, 9}, {11, 9}
228};
229
230/* Decoding table for dct_dc_size_chrominance */
231dct_dc_size_entry dct_dc_size_chrominance[32] =
232{ {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2},
233 {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2},
234 {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2},
235 {3, 3}, {3, 3}, {3, 3}, {3, 3}, {4, 4}, {4, 4}, {5, 5}, {(unsigned int)MPGERROR, 0}
236};
237
238dct_dc_size_entry dct_dc_size_chrominance1[32] =
239{ {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6},
240 {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6},
241 {7, 7}, {7, 7}, {7, 7}, {7, 7}, {7, 7}, {7, 7}, {7, 7}, {7, 7},
242 {8, 8}, {8, 8}, {8, 8}, {8, 8}, {9, 9}, {9, 9}, {10, 10}, {11, 10}
243};
244
245/* DCT coeff tables. */
246
247unsigned short int dct_coeff_tbl_0[256] =
248{
2490xffff, 0xffff, 0xffff, 0xffff,
2500xffff, 0xffff, 0xffff, 0xffff,
2510xffff, 0xffff, 0xffff, 0xffff,
2520xffff, 0xffff, 0xffff, 0xffff,
2530x052f, 0x051f, 0x050f, 0x04ff,
2540x183f, 0x402f, 0x3c2f, 0x382f,
2550x342f, 0x302f, 0x2c2f, 0x7c1f,
2560x781f, 0x741f, 0x701f, 0x6c1f,
2570x028e, 0x028e, 0x027e, 0x027e,
2580x026e, 0x026e, 0x025e, 0x025e,
2590x024e, 0x024e, 0x023e, 0x023e,
2600x022e, 0x022e, 0x021e, 0x021e,
2610x020e, 0x020e, 0x04ee, 0x04ee,
2620x04de, 0x04de, 0x04ce, 0x04ce,
2630x04be, 0x04be, 0x04ae, 0x04ae,
2640x049e, 0x049e, 0x048e, 0x048e,
2650x01fd, 0x01fd, 0x01fd, 0x01fd,
2660x01ed, 0x01ed, 0x01ed, 0x01ed,
2670x01dd, 0x01dd, 0x01dd, 0x01dd,
2680x01cd, 0x01cd, 0x01cd, 0x01cd,
2690x01bd, 0x01bd, 0x01bd, 0x01bd,
2700x01ad, 0x01ad, 0x01ad, 0x01ad,
2710x019d, 0x019d, 0x019d, 0x019d,
2720x018d, 0x018d, 0x018d, 0x018d,
2730x017d, 0x017d, 0x017d, 0x017d,
2740x016d, 0x016d, 0x016d, 0x016d,
2750x015d, 0x015d, 0x015d, 0x015d,
2760x014d, 0x014d, 0x014d, 0x014d,
2770x013d, 0x013d, 0x013d, 0x013d,
2780x012d, 0x012d, 0x012d, 0x012d,
2790x011d, 0x011d, 0x011d, 0x011d,
2800x010d, 0x010d, 0x010d, 0x010d,
2810x282c, 0x282c, 0x282c, 0x282c,
2820x282c, 0x282c, 0x282c, 0x282c,
2830x242c, 0x242c, 0x242c, 0x242c,
2840x242c, 0x242c, 0x242c, 0x242c,
2850x143c, 0x143c, 0x143c, 0x143c,
2860x143c, 0x143c, 0x143c, 0x143c,
2870x0c4c, 0x0c4c, 0x0c4c, 0x0c4c,
2880x0c4c, 0x0c4c, 0x0c4c, 0x0c4c,
2890x085c, 0x085c, 0x085c, 0x085c,
2900x085c, 0x085c, 0x085c, 0x085c,
2910x047c, 0x047c, 0x047c, 0x047c,
2920x047c, 0x047c, 0x047c, 0x047c,
2930x046c, 0x046c, 0x046c, 0x046c,
2940x046c, 0x046c, 0x046c, 0x046c,
2950x00fc, 0x00fc, 0x00fc, 0x00fc,
2960x00fc, 0x00fc, 0x00fc, 0x00fc,
2970x00ec, 0x00ec, 0x00ec, 0x00ec,
2980x00ec, 0x00ec, 0x00ec, 0x00ec,
2990x00dc, 0x00dc, 0x00dc, 0x00dc,
3000x00dc, 0x00dc, 0x00dc, 0x00dc,
3010x00cc, 0x00cc, 0x00cc, 0x00cc,
3020x00cc, 0x00cc, 0x00cc, 0x00cc,
3030x681c, 0x681c, 0x681c, 0x681c,
3040x681c, 0x681c, 0x681c, 0x681c,
3050x641c, 0x641c, 0x641c, 0x641c,
3060x641c, 0x641c, 0x641c, 0x641c,
3070x601c, 0x601c, 0x601c, 0x601c,
3080x601c, 0x601c, 0x601c, 0x601c,
3090x5c1c, 0x5c1c, 0x5c1c, 0x5c1c,
3100x5c1c, 0x5c1c, 0x5c1c, 0x5c1c,
3110x581c, 0x581c, 0x581c, 0x581c,
3120x581c, 0x581c, 0x581c, 0x581c,
313};
314
315unsigned short int dct_coeff_tbl_1[16] =
316{
3170x00bb, 0x202b, 0x103b, 0x00ab,
3180x084b, 0x1c2b, 0x541b, 0x501b,
3190x009b, 0x4c1b, 0x481b, 0x045b,
3200x0c3b, 0x008b, 0x182b, 0x441b,
321};
322
323unsigned short int dct_coeff_tbl_2[4] =
324{
3250x4019, 0x1429, 0x0079, 0x0839,
326};
327
328unsigned short int dct_coeff_tbl_3[4] =
329{
3300x0449, 0x3c19, 0x3819, 0x1029,
331};
332
333unsigned short int dct_coeff_next[256] =
334{
3350xffff, 0xffff, 0xffff, 0xffff,
3360xf7d5, 0xf7d5, 0xf7d5, 0xf7d5,
3370x0826, 0x0826, 0x2416, 0x2416,
3380x0046, 0x0046, 0x2016, 0x2016,
3390x1c15, 0x1c15, 0x1c15, 0x1c15,
3400x1815, 0x1815, 0x1815, 0x1815,
3410x0425, 0x0425, 0x0425, 0x0425,
3420x1415, 0x1415, 0x1415, 0x1415,
3430x3417, 0x0067, 0x3017, 0x2c17,
3440x0c27, 0x0437, 0x0057, 0x2817,
3450x0034, 0x0034, 0x0034, 0x0034,
3460x0034, 0x0034, 0x0034, 0x0034,
3470x1014, 0x1014, 0x1014, 0x1014,
3480x1014, 0x1014, 0x1014, 0x1014,
3490x0c14, 0x0c14, 0x0c14, 0x0c14,
3500x0c14, 0x0c14, 0x0c14, 0x0c14,
3510x0023, 0x0023, 0x0023, 0x0023,
3520x0023, 0x0023, 0x0023, 0x0023,
3530x0023, 0x0023, 0x0023, 0x0023,
3540x0023, 0x0023, 0x0023, 0x0023,
3550x0813, 0x0813, 0x0813, 0x0813,
3560x0813, 0x0813, 0x0813, 0x0813,
3570x0813, 0x0813, 0x0813, 0x0813,
3580x0813, 0x0813, 0x0813, 0x0813,
3590x0412, 0x0412, 0x0412, 0x0412,
3600x0412, 0x0412, 0x0412, 0x0412,
3610x0412, 0x0412, 0x0412, 0x0412,
3620x0412, 0x0412, 0x0412, 0x0412,
3630x0412, 0x0412, 0x0412, 0x0412,
3640x0412, 0x0412, 0x0412, 0x0412,
3650x0412, 0x0412, 0x0412, 0x0412,
3660x0412, 0x0412, 0x0412, 0x0412,
3670xfbe1, 0xfbe1, 0xfbe1, 0xfbe1,
3680xfbe1, 0xfbe1, 0xfbe1, 0xfbe1,
3690xfbe1, 0xfbe1, 0xfbe1, 0xfbe1,
3700xfbe1, 0xfbe1, 0xfbe1, 0xfbe1,
3710xfbe1, 0xfbe1, 0xfbe1, 0xfbe1,
3720xfbe1, 0xfbe1, 0xfbe1, 0xfbe1,
3730xfbe1, 0xfbe1, 0xfbe1, 0xfbe1,
3740xfbe1, 0xfbe1, 0xfbe1, 0xfbe1,
3750xfbe1, 0xfbe1, 0xfbe1, 0xfbe1,
3760xfbe1, 0xfbe1, 0xfbe1, 0xfbe1,
3770xfbe1, 0xfbe1, 0xfbe1, 0xfbe1,
3780xfbe1, 0xfbe1, 0xfbe1, 0xfbe1,
3790xfbe1, 0xfbe1, 0xfbe1, 0xfbe1,
3800xfbe1, 0xfbe1, 0xfbe1, 0xfbe1,
3810xfbe1, 0xfbe1, 0xfbe1, 0xfbe1,
3820xfbe1, 0xfbe1, 0xfbe1, 0xfbe1,
3830x0011, 0x0011, 0x0011, 0x0011,
3840x0011, 0x0011, 0x0011, 0x0011,
3850x0011, 0x0011, 0x0011, 0x0011,
3860x0011, 0x0011, 0x0011, 0x0011,
3870x0011, 0x0011, 0x0011, 0x0011,
3880x0011, 0x0011, 0x0011, 0x0011,
3890x0011, 0x0011, 0x0011, 0x0011,
3900x0011, 0x0011, 0x0011, 0x0011,
3910x0011, 0x0011, 0x0011, 0x0011,
3920x0011, 0x0011, 0x0011, 0x0011,
3930x0011, 0x0011, 0x0011, 0x0011,
3940x0011, 0x0011, 0x0011, 0x0011,
3950x0011, 0x0011, 0x0011, 0x0011,
3960x0011, 0x0011, 0x0011, 0x0011,
3970x0011, 0x0011, 0x0011, 0x0011,
3980x0011, 0x0011, 0x0011, 0x0011,
399};
400
401unsigned short int dct_coeff_first[256] =
402{
4030xffff, 0xffff, 0xffff, 0xffff,
4040xf7d5, 0xf7d5, 0xf7d5, 0xf7d5,
4050x0826, 0x0826, 0x2416, 0x2416,
4060x0046, 0x0046, 0x2016, 0x2016,
4070x1c15, 0x1c15, 0x1c15, 0x1c15,
4080x1815, 0x1815, 0x1815, 0x1815,
4090x0425, 0x0425, 0x0425, 0x0425,
4100x1415, 0x1415, 0x1415, 0x1415,
4110x3417, 0x0067, 0x3017, 0x2c17,
4120x0c27, 0x0437, 0x0057, 0x2817,
4130x0034, 0x0034, 0x0034, 0x0034,
4140x0034, 0x0034, 0x0034, 0x0034,
4150x1014, 0x1014, 0x1014, 0x1014,
4160x1014, 0x1014, 0x1014, 0x1014,
4170x0c14, 0x0c14, 0x0c14, 0x0c14,
4180x0c14, 0x0c14, 0x0c14, 0x0c14,
4190x0023, 0x0023, 0x0023, 0x0023,
4200x0023, 0x0023, 0x0023, 0x0023,
4210x0023, 0x0023, 0x0023, 0x0023,
4220x0023, 0x0023, 0x0023, 0x0023,
4230x0813, 0x0813, 0x0813, 0x0813,
4240x0813, 0x0813, 0x0813, 0x0813,
4250x0813, 0x0813, 0x0813, 0x0813,
4260x0813, 0x0813, 0x0813, 0x0813,
4270x0412, 0x0412, 0x0412, 0x0412,
4280x0412, 0x0412, 0x0412, 0x0412,
4290x0412, 0x0412, 0x0412, 0x0412,
4300x0412, 0x0412, 0x0412, 0x0412,
4310x0412, 0x0412, 0x0412, 0x0412,
4320x0412, 0x0412, 0x0412, 0x0412,
4330x0412, 0x0412, 0x0412, 0x0412,
4340x0412, 0x0412, 0x0412, 0x0412,
4350x0010, 0x0010, 0x0010, 0x0010,
4360x0010, 0x0010, 0x0010, 0x0010,
4370x0010, 0x0010, 0x0010, 0x0010,
4380x0010, 0x0010, 0x0010, 0x0010,
4390x0010, 0x0010, 0x0010, 0x0010,
4400x0010, 0x0010, 0x0010, 0x0010,
4410x0010, 0x0010, 0x0010, 0x0010,
4420x0010, 0x0010, 0x0010, 0x0010,
4430x0010, 0x0010, 0x0010, 0x0010,
4440x0010, 0x0010, 0x0010, 0x0010,
4450x0010, 0x0010, 0x0010, 0x0010,
4460x0010, 0x0010, 0x0010, 0x0010,
4470x0010, 0x0010, 0x0010, 0x0010,
4480x0010, 0x0010, 0x0010, 0x0010,
4490x0010, 0x0010, 0x0010, 0x0010,
4500x0010, 0x0010, 0x0010, 0x0010,
4510x0010, 0x0010, 0x0010, 0x0010,
4520x0010, 0x0010, 0x0010, 0x0010,
4530x0010, 0x0010, 0x0010, 0x0010,
4540x0010, 0x0010, 0x0010, 0x0010,
4550x0010, 0x0010, 0x0010, 0x0010,
4560x0010, 0x0010, 0x0010, 0x0010,
4570x0010, 0x0010, 0x0010, 0x0010,
4580x0010, 0x0010, 0x0010, 0x0010,
4590x0010, 0x0010, 0x0010, 0x0010,
4600x0010, 0x0010, 0x0010, 0x0010,
4610x0010, 0x0010, 0x0010, 0x0010,
4620x0010, 0x0010, 0x0010, 0x0010,
4630x0010, 0x0010, 0x0010, 0x0010,
4640x0010, 0x0010, 0x0010, 0x0010,
4650x0010, 0x0010, 0x0010, 0x0010,
4660x0010, 0x0010, 0x0010, 0x0010,
467};
468
469/* Macro for filling up the decoding table for mb_addr_inc */
470#define ASSIGN1(start, end, step, val, num) \
471 for (i = start; i < end; i+= step) { \
472 for (j = 0; j < step; j++) { \
473 mb_addr_inc[i+j].value = val; \
474 mb_addr_inc[i+j].num_bits = num; \
475 } \
476 val--; \
477 }
478
479
480
481
482/*
483 *--------------------------------------------------------------
484 *
485 * init_mb_addr_inc --
486 *
487 * Initialize the VLC decoding table for macro_block_address_increment
488 *
489 * Results:
490 * The decoding table for macro_block_address_increment will
491 * be filled; illegal values will be filled as ERROR.
492 *
493 * Side effects:
494 * The global array mb_addr_inc will be filled.
495 *
496 *--------------------------------------------------------------
497 */
498static void
499init_mb_addr_inc()
500{
501 int i, j, val;
502
503 for (i = 0; i < 8; i++) {
504 mb_addr_inc[i].value = MPGERROR;
505 mb_addr_inc[i].num_bits = 0;
506 }
507
508 mb_addr_inc[8].value = MACRO_BLOCK_ESCAPE;
509 mb_addr_inc[8].num_bits = 11;
510
511 for (i = 9; i < 15; i++) {
512 mb_addr_inc[i].value = MPGERROR;
513 mb_addr_inc[i].num_bits = 0;
514 }
515
516 mb_addr_inc[15].value = MACRO_BLOCK_STUFFING;
517 mb_addr_inc[15].num_bits = 11;
518
519 for (i = 16; i < 24; i++) {
520 mb_addr_inc[i].value = MPGERROR;
521 mb_addr_inc[i].num_bits = 0;
522 }
523
524 val = 33;
525
526 ASSIGN1(24, 36, 1, val, 11);
527 ASSIGN1(36, 48, 2, val, 10);
528 ASSIGN1(48, 96, 8, val, 8);
529 ASSIGN1(96, 128, 16, val, 7);
530 ASSIGN1(128, 256, 64, val, 5);
531 ASSIGN1(256, 512, 128, val, 4);
532 ASSIGN1(512, 1024, 256, val, 3);
533 ASSIGN1(1024, 2048, 1024, val, 1);
534}
535
536
537/* Macro for filling up the decoding table for mb_type */
538#define ASSIGN2(start, end, quant, motion_forward, motion_backward, pattern, intra, num, mb_type) \
539 for (i = start; i < end; i ++) { \
540 mb_type[i].mb_quant = quant; \
541 mb_type[i].mb_motion_forward = motion_forward; \
542 mb_type[i].mb_motion_backward = motion_backward; \
543 mb_type[i].mb_pattern = pattern; \
544 mb_type[i].mb_intra = intra; \
545 mb_type[i].num_bits = num; \
546 }
547
548
549
550/*
551 *--------------------------------------------------------------
552 *
553 * init_mb_type_P --
554 *
555 * Initialize the VLC decoding table for macro_block_type in
556 * predictive-coded pictures.
557 *
558 * Results:
559 * The decoding table for macro_block_type in predictive-coded
560 * pictures will be filled; illegal values will be filled as ERROR.
561 *
562 * Side effects:
563 * The global array mb_type_P will be filled.
564 *
565 *--------------------------------------------------------------
566 */
567static void
568init_mb_type_P()
569{
570 int i;
571
572 mb_type_P[0].mb_quant = mb_type_P[0].mb_motion_forward
573 = mb_type_P[0].mb_motion_backward = mb_type_P[0].mb_pattern
574 = mb_type_P[0].mb_intra = (unsigned int) MPGERROR;
575 mb_type_P[0].num_bits = 0;
576
577 ASSIGN2(1, 2, 1, 0, 0, 0, 1, 6, mb_type_P)
578 ASSIGN2(2, 4, 1, 0, 0, 1, 0, 5, mb_type_P)
579 ASSIGN2(4, 6, 1, 1, 0, 1, 0, 5, mb_type_P);
580 ASSIGN2(6, 8, 0, 0, 0, 0, 1, 5, mb_type_P);
581 ASSIGN2(8, 16, 0, 1, 0, 0, 0, 3, mb_type_P);
582 ASSIGN2(16, 32, 0, 0, 0, 1, 0, 2, mb_type_P);
583 ASSIGN2(32, 64, 0, 1, 0, 1, 0, 1, mb_type_P);
584}
585
586
587
588
589/*
590 *--------------------------------------------------------------
591 *
592 * init_mb_type_B --
593 *
594 * Initialize the VLC decoding table for macro_block_type in
595 * bidirectionally-coded pictures.
596 *
597 * Results:
598 * The decoding table for macro_block_type in bidirectionally-coded
599 * pictures will be filled; illegal values will be filled as ERROR.
600 *
601 * Side effects:
602 * The global array mb_type_B will be filled.
603 *
604 *--------------------------------------------------------------
605 */
606static void
607init_mb_type_B()
608{
609 int i;
610
611 mb_type_B[0].mb_quant = mb_type_B[0].mb_motion_forward
612 = mb_type_B[0].mb_motion_backward = mb_type_B[0].mb_pattern
613 = mb_type_B[0].mb_intra = (unsigned int) MPGERROR;
614 mb_type_B[0].num_bits = 0;
615
616 ASSIGN2(1, 2, 1, 0, 0, 0, 1, 6, mb_type_B);
617 ASSIGN2(2, 3, 1, 0, 1, 1, 0, 6, mb_type_B);
618 ASSIGN2(3, 4, 1, 1, 0, 1, 0, 6, mb_type_B);
619 ASSIGN2(4, 6, 1, 1, 1, 1, 0, 5, mb_type_B);
620 ASSIGN2(6, 8, 0, 0, 0, 0, 1, 5, mb_type_B);
621 ASSIGN2(8, 12, 0, 1, 0, 0, 0, 4, mb_type_B);
622 ASSIGN2(12, 16, 0, 1, 0, 1, 0, 4, mb_type_B);
623 ASSIGN2(16, 24, 0, 0, 1, 0, 0, 3, mb_type_B);
624 ASSIGN2(24, 32, 0, 0, 1, 1, 0, 3, mb_type_B);
625 ASSIGN2(32, 48, 0, 1, 1, 0, 0, 2, mb_type_B);
626 ASSIGN2(48, 64, 0, 1, 1, 1, 0, 2, mb_type_B);
627}
628
629
630/* Macro for filling up the decoding tables for motion_vectors */
631#define ASSIGN3(start, end, step, val, num) \
632 for (i = start; i < end; i+= step) { \
633 for (j = 0; j < step / 2; j++) { \
634 motion_vectors[i+j].code = val; \
635 motion_vectors[i+j].num_bits = num; \
636 } \
637 for (j = step / 2; j < step; j++) { \
638 motion_vectors[i+j].code = -val; \
639 motion_vectors[i+j].num_bits = num; \
640 } \
641 val--; \
642 }
643
644
645
646/*
647 *--------------------------------------------------------------
648 *
649 * init_motion_vectors --
650 *
651 * Initialize the VLC decoding table for the various motion
652 * vectors, including motion_horizontal_forward_code,
653 * motion_vertical_forward_code, motion_horizontal_backward_code,
654 * and motion_vertical_backward_code.
655 *
656 * Results:
657 * The decoding table for the motion vectors will be filled;
658 * illegal values will be filled as ERROR.
659 *
660 * Side effects:
661 * The global array motion_vector will be filled.
662 *
663 *--------------------------------------------------------------
664 */
665static void
666init_motion_vectors()
667{
668 int i, j, val = 16;
669
670 for (i = 0; i < 24; i++) {
671 motion_vectors[i].code = MPGERROR;
672 motion_vectors[i].num_bits = 0;
673 }
674
675 ASSIGN3(24, 36, 2, val, 11);
676 ASSIGN3(36, 48, 4, val, 10);
677 ASSIGN3(48, 96, 16, val, 8);
678 ASSIGN3(96, 128, 32, val, 7);
679 ASSIGN3(128, 256, 128, val, 5);
680 ASSIGN3(256, 512, 256, val, 4);
681 ASSIGN3(512, 1024, 512, val, 3);
682 ASSIGN3(1024, 2048, 1024, val, 1);
683}
684
685
686
687extern void init_pre_idct();
688
689
690/*
691 *--------------------------------------------------------------
692 *
693 * init_tables --
694 *
695 * Initialize all the tables for VLC decoding; this must be
696 * called when the system is set up before any decoding can
697 * take place.
698 *
699 * Results:
700 * All the decoding tables will be filled accordingly.
701 *
702 * Side effects:
703 * The corresponding global array for each decoding table
704 * will be filled.
705 *
706 *--------------------------------------------------------------
707 */
708void
709init_tables()
710{
711
712 init_mb_addr_inc();
713 init_mb_type_P();
714 init_mb_type_B();
715 init_motion_vectors();
716
717#ifdef FLOATDCT
718 if (qualityFlag)
719 init_float_idct();
720 else
721#endif
722 init_pre_idct();
723
724}
725
726#if OLDCODE
727
728/*
729 *--------------------------------------------------------------
730 *
731 * DecodeDCTDCSizeLum --
732 *
733 * Huffman Decoder for dct_dc_size_luminance; location where
734 * the result of decoding will be placed is passed as argument.
735 * The decoded values are obtained by doing a table lookup on
736 * dct_dc_size_luminance.
737 *
738 * Results:
739 * The decoded value for dct_dc_size_luminance or ERROR for
740 * unbound values will be placed in the location specified.
741 *
742 * Side effects:
743 * Bit stream is irreversibly parsed.
744 *
745 *--------------------------------------------------------------
746 */
747void
748decodeDCTDCSizeLum(value)
749unsigned int *value;
750{
751 unsigned int index;
752
753 show_bits5(index);
754
755 if (index < 31) {
756 *value = dct_dc_size_luminance[index].value;
757 flush_bits(dct_dc_size_luminance[index].num_bits);
758 }
759 else {
760 show_bits9(index);
761 index -= 0x1f0;
762 *value = dct_dc_size_luminance1[index].value;
763 flush_bits(dct_dc_size_luminance1[index].num_bits);
764 }
765}
766
767
768
769
770/*
771 *--------------------------------------------------------------
772 *
773 * DecodeDCTDCSizeChrom --
774 *
775 * Huffman Decoder for dct_dc_size_chrominance; location where
776 * the result of decoding will be placed is passed as argument.
777 * The decoded values are obtained by doing a table lookup on
778 * dct_dc_size_chrominance.
779 *
780 * Results:
781 * The decoded value for dct_dc_size_chrominance or ERROR for
782 * unbound values will be placed in the location specified.
783 *
784 * Side effects:
785 * Bit stream is irreversibly parsed.
786 *
787 *--------------------------------------------------------------
788 */
789void
790decodeDCTDCSizeChrom(value)
791unsigned int *value;
792{
793 unsigned int index;
794
795 show_bits5(index);
796
797 if (index < 31) {
798 *value = dct_dc_size_chrominance[index].value;
799 flush_bits(dct_dc_size_chrominance[index].num_bits);
800 }
801 else {
802 show_bits10(index);
803 index -= 0x3e0;
804 *value = dct_dc_size_chrominance1[index].value;
805 flush_bits(dct_dc_size_chrominance1[index].num_bits);
806 }
807}
808
809
810
811/*
812 *--------------------------------------------------------------
813 *
814 * decodeDCTCoeff --
815 *
816 * Huffman Decoder for dct_coeff_first and dct_coeff_next;
817 * locations where the results of decoding: run and level, are to
818 * be placed and also the type of DCT coefficients, either
819 * dct_coeff_first or dct_coeff_next, are being passed as argument.
820 *
821 * The decoder first examines the next 8 bits in the input stream,
822 * and perform according to the following cases:
823 *
824 * '0000 0000' - examine 8 more bits (i.e. 16 bits total) and
825 * perform a table lookup on dct_coeff_tbl_0.
826 * One more bit is then examined to determine the sign
827 * of level.
828 *
829 * '0000 0001' - examine 4 more bits (i.e. 12 bits total) and
830 * perform a table lookup on dct_coeff_tbl_1.
831 * One more bit is then examined to determine the sign
832 * of level.
833 *
834 * '0000 0010' - examine 2 more bits (i.e. 10 bits total) and
835 * perform a table lookup on dct_coeff_tbl_2.
836 * One more bit is then examined to determine the sign
837 * of level.
838 *
839 * '0000 0011' - examine 2 more bits (i.e. 10 bits total) and
840 * perform a table lookup on dct_coeff_tbl_3.
841 * One more bit is then examined to determine the sign
842 * of level.
843 *
844 * otherwise - perform a table lookup on dct_coeff_tbl. If the
845 * value of run is not ESCAPE, extract one more bit
846 * to determine the sign of level; otherwise 6 more
847 * bits will be extracted to obtain the actual value
848 * of run , and then 8 or 16 bits to get the value of level.
849 *
850 *
851 *
852 * Results:
853 * The decoded values of run and level or ERROR for unbound values
854 * are placed in the locations specified.
855 *
856 * Side effects:
857 * Bit stream is irreversibly parsed.
858 *
859 *--------------------------------------------------------------
860 */
861static void
862decodeDCTCoeff(dct_coeff_tbl, run, level)
863unsigned short int *dct_coeff_tbl;
864unsigned int *run;
865int *level;
866{
867 unsigned int temp, index /*, num_bits */;
868 unsigned int value, next32bits, flushed;
869
870 /*
871 * Grab the next 32 bits and use it to improve performance of
872 * getting the bits to parse. Thus, calls are translated as:
873 *
874 * show_bitsX <--> next32bits >> (32-X)
875 * get_bitsX <--> val = next32bits >> (32-flushed-X);
876 * flushed += X;
877 * next32bits &= bitMask[flushed];
878 * flush_bitsX <--> flushed += X;
879 * next32bits &= bitMask[flushed];
880 *
881 */
882 show_bits32(next32bits);
883 flushed = 0;
884
885 /* show_bits8(index); */
886 index = next32bits >> 24;
887
888 if (index > 3) {
889 value = dct_coeff_tbl[index];
890 *run = (value & RUN_MASK) >> RUN_SHIFT;
891 if (*run == END_OF_BLOCK) {
892 *level = END_OF_BLOCK;
893 }
894 else {
895 /* num_bits = (value & NUM_MASK) + 1; */
896 /* flush_bits(num_bits); */
897 flushed = (value & NUM_MASK) + 1;
898 next32bits &= bitMask[flushed];
899 if (*run != ESCAPE) {
900 *level = (value & LEVEL_MASK) >> LEVEL_SHIFT;
901 /* get_bits1(value); */
902 /* if (value) *level = -*level; */
903 if (next32bits >> (31-flushed)) *level = -*level;
904 flushed++;
905 /* next32bits &= bitMask[flushed]; last op before update */
906 }
907 else { /* *run == ESCAPE */
908 /* get_bits14(temp); */
909 temp = next32bits >> (18-flushed);
910 flushed += 14;
911 next32bits &= bitMask[flushed];
912 *run = temp >> 8;
913 temp &= 0xff;
914 if (temp == 0) {
915 /* get_bits8(*level); */
916 *level = next32bits >> (24-flushed);
917 flushed += 8;
918 /* next32bits &= bitMask[flushed]; last op before update */
919 assert(*level >= 128);
920 } else if (temp != 128) {
921 /* Grab sign bit */
922 *level = ((int) (temp << 24)) >> 24;
923 } else {
924 /* get_bits8(*level); */
925 *level = next32bits >> (24-flushed);
926 flushed += 8;
927 /* next32bits &= bitMask[flushed]; last op before update */
928 *level = *level - 256;
929 assert(*level <= -128 && *level >= -255);
930 }
931 }
932 /* Update bitstream... */
933 flush_bits(flushed);
934 }
935 }
936 else {
937 if (index == 2) {
938 /* show_bits10(index); */
939 index = next32bits >> 22;
940 value = dct_coeff_tbl_2[index & 3];
941 }
942 else if (index == 3) {
943 /* show_bits10(index); */
944 index = next32bits >> 22;
945 value = dct_coeff_tbl_3[index & 3];
946 }
947 else if (index) { /* index == 1 */
948 /* show_bits12(index); */
949 index = next32bits >> 20;
950 value = dct_coeff_tbl_1[index & 15];
951 }
952 else { /* index == 0 */
953 /* show_bits16(index); */
954 index = next32bits >> 16;
955 value = dct_coeff_tbl_0[index & 255];
956 }
957 *run = (value & RUN_MASK) >> RUN_SHIFT;
958 *level = (value & LEVEL_MASK) >> LEVEL_SHIFT;
959
960 /*
961 * Fold these operations together to make it fast...
962 */
963 /* num_bits = (value & NUM_MASK) + 1; */
964 /* flush_bits(num_bits); */
965 /* get_bits1(value); */
966 /* if (value) *level = -*level; */
967
968 flushed = (value & NUM_MASK) + 2;
969 if ((next32bits >> (32-flushed)) & 0x1) *level = -*level;
970
971 /* Update bitstream ... */
972 flush_bits(flushed);
973 }
974}
975
976
977/*
978 *--------------------------------------------------------------
979 *
980 * decodeDCTCoeffFirst --
981 *
982 * Huffman Decoder for dct_coeff_first. Locations for the
983 * decoded results: run and level, are being passed as
984 * arguments. Actual work is being done by calling DecodeDCTCoeff,
985 * with the table dct_coeff_first.
986 *
987 * Results:
988 * The decoded values of run and level for dct_coeff_first or
989 * ERROR for unbound values are placed in the locations given.
990 *
991 * Side effects:
992 * Bit stream is irreversibly parsed.
993 *
994 *--------------------------------------------------------------
995 */
996void
997decodeDCTCoeffFirst(run, level)
998unsigned int *run;
999int *level;
1000{
1001 decodeDCTCoeff(dct_coeff_first, run, level);
1002}
1003
1004
1005
1006
1007/*
1008 *--------------------------------------------------------------
1009 *
1010 * decodeDCTCoeffNext --
1011 *
1012 * Huffman Decoder for dct_coeff_first. Locations for the
1013 * decoded results: run and level, are being passed as
1014 * arguments. Actual work is being done by calling DecodeDCTCoeff,
1015 * with the table dct_coeff_next.
1016 *
1017 * Results:
1018 * The decoded values of run and level for dct_coeff_next or
1019 * ERROR for unbound values are placed in the locations given.
1020 *
1021 * Side effects:
1022 * Bit stream is irreversibly parsed.
1023 *
1024 *--------------------------------------------------------------
1025 */
1026void
1027decodeDCTCoeffNext(run, level)
1028unsigned int *run;
1029int *level;
1030{
1031 decodeDCTCoeff(dct_coeff_next, run, level);
1032}
1033#endif
1034
1035static int
1036number_of_bits_set(a)
1037unsigned long a;
1038{
1039 if(!a) return 0;
1040 if(a & 1) return 1 + number_of_bits_set(a >> 1);
1041 return(number_of_bits_set(a >> 1));
1042}
1043
1044/*
1045 * How many 0 bits are there at least significant end of longword.
1046 * Low performance, do not call often.
1047 */
1048static int
1049free_bits_at_bottom(a)
1050unsigned long a;
1051{
1052 /* assume char is 8 bits */
1053 if(!a) return sizeof(unsigned long) * 8;
1054 if(((long)a) & 1l) return 0;
1055 return 1 + free_bits_at_bottom ( a >> 1);
1056}
1057
1058static int *L_tab=NULL, *Cr_r_tab=NULL, *Cr_g_tab=NULL, *Cb_g_tab=NULL,
1059 *Cb_b_tab=NULL;
1060
1061/*
1062 * We define tables that convert a color value between -256 and 512
1063 * into the R, G and B parts of the pixel. The normal range is 0-255.
1064 */
1065
1066static long *r_2_pix=NULL;
1067static long *g_2_pix=NULL;
1068static long *b_2_pix=NULL;
1069static long *r_2_pix_alloc=NULL;
1070static long *g_2_pix_alloc=NULL;
1071static long *b_2_pix_alloc=NULL;
1072
1073
1074/*
1075 *--------------------------------------------------------------
1076 *
1077 * InitColor16Dither --
1078 *
1079 * To get rid of the multiply and other conversions in color
1080 * dither, we use a lookup table.
1081 *
1082 * Results:
1083 * None.
1084 *
1085 * Side effects:
1086 * The lookup tables are initialized.
1087 *
1088 *--------------------------------------------------------------
1089 */
1090
1091void
1092InitColorDither(thirty2)
1093int thirty2;
1094{
1095 /*
1096 * misuse of the wpixel array for the pixel masks. Note that this
1097 * implies that the window is created before this routine is called
1098 */
1099 unsigned long red_mask = 0xff;
1100 unsigned long green_mask = 0xff00;
1101 unsigned long blue_mask = 0xff0000;
1102
1103 int CR, CB, i;
1104
1105 if (L_tab==NULL)
1106 L_tab = (int *)malloc(256*sizeof(int));
1107 if (Cr_r_tab==NULL)
1108 Cr_r_tab = (int *)malloc(256*sizeof(int));
1109 if (Cr_g_tab==NULL)
1110 Cr_g_tab = (int *)malloc(256*sizeof(int));
1111 if (Cb_g_tab==NULL)
1112 Cb_g_tab = (int *)malloc(256*sizeof(int));
1113 if (Cb_b_tab==NULL)
1114 Cb_b_tab = (int *)malloc(256*sizeof(int));
1115
1116 if (r_2_pix_alloc==NULL)
1117 r_2_pix_alloc = (long *)malloc(768*sizeof(long));
1118 if (g_2_pix_alloc==NULL)
1119 g_2_pix_alloc = (long *)malloc(768*sizeof(long));
1120 if (b_2_pix_alloc==NULL)
1121 b_2_pix_alloc = (long *)malloc(768*sizeof(long));
1122
1123 if (L_tab == NULL ||
1124 Cr_r_tab == NULL ||
1125 Cr_g_tab == NULL ||
1126 Cb_g_tab == NULL ||
1127 Cb_b_tab == NULL ||
1128 r_2_pix_alloc == NULL ||
1129 g_2_pix_alloc == NULL ||
1130 b_2_pix_alloc == NULL) {
1131 fprintf(stderr, "Could not get enough memory in InitColorDither\n");
1132 exit(1);
1133 }
1134
1135 for (i=0; i<256; i++) {
1136 L_tab[i] = i;
1137 CB = CR = i;
1138
1139 CB -= 128; CR -= 128;
1140 Cr_r_tab[i] = (0.419/0.299) * CR;
1141 Cr_g_tab[i] = -(0.299/0.419) * CR;
1142 Cb_g_tab[i] = -(0.114/0.331) * CB;
1143 Cb_b_tab[i] = (0.587/0.331) * CB;
1144 }
1145
1146 /*
1147 * Set up entries 0-255 in rgb-to-pixel value tables.
1148 */
1149 for (i = 0; i < 256; i++) {
1150 r_2_pix_alloc[i + 256] = i >> (8 - number_of_bits_set(red_mask));
1151 r_2_pix_alloc[i + 256] <<= free_bits_at_bottom(red_mask);
1152 g_2_pix_alloc[i + 256] = i >> (8 - number_of_bits_set(green_mask));
1153 g_2_pix_alloc[i + 256] <<= free_bits_at_bottom(green_mask);
1154 b_2_pix_alloc[i + 256] = i >> (8 - number_of_bits_set(blue_mask));
1155 b_2_pix_alloc[i + 256] <<= free_bits_at_bottom(blue_mask);
1156 /*
1157 * If we have 16-bit output depth, then we double the value
1158 * in the top word. This means that we can write out both
1159 * pixels in the pixel doubling mode with one op. It is
1160 * harmless in the normal case as storing a 32-bit value
1161 * through a short pointer will lose the top bits anyway.
1162 * A similar optimisation for Alpha for 64 bit has been
1163 * prepared for, but is not yet implemented.
1164 */
1165 }
1166
1167 /*
1168 * Spread out the values we have to the rest of the array so that
1169 * we do not need to check for overflow.
1170 */
1171 for (i = 0; i < 256; i++) {
1172 r_2_pix_alloc[i] = r_2_pix_alloc[256];
1173 r_2_pix_alloc[i+ 512] = r_2_pix_alloc[511];
1174 g_2_pix_alloc[i] = g_2_pix_alloc[256];
1175 g_2_pix_alloc[i+ 512] = g_2_pix_alloc[511];
1176 b_2_pix_alloc[i] = b_2_pix_alloc[256];
1177 b_2_pix_alloc[i+ 512] = b_2_pix_alloc[511];
1178 }
1179
1180 r_2_pix = r_2_pix_alloc + 256;
1181 g_2_pix = g_2_pix_alloc + 256;
1182 b_2_pix = b_2_pix_alloc + 256;
1183
1184}
1185
1186
1187/*
1188 *--------------------------------------------------------------
1189 *
1190 * Color32DitherImage --
1191 *
1192 * Converts image into 32 bit color (or 24-bit non-packed).
1193 *
1194 * Results:
1195 * None.
1196 *
1197 * Side effects:
1198 * None.
1199 *
1200 *--------------------------------------------------------------
1201 */
1202
1203/*
1204 * This is a copysoft version of the function above with ints instead
1205 * of shorts to cause a 4-byte pixel size
1206 */
1207
1208void
1209Color32DitherImage(lum, cr, cb, out, rows, cols)
1210 unsigned char *lum;
1211 unsigned char *cr;
1212 unsigned char *cb;
1213 unsigned char *out;
1214 int cols, rows;
1215
1216{
1217
1218 int L, CR, CB;
1219 unsigned int *row1, *row2;
1220 unsigned char *lum2;
1221 int x, y;
1222 int cr_r;
1223 int cr_g;
1224 int cb_g;
1225 int cb_b;
1226 int cols_2 = cols / 2;
1227
1228 row1 = (unsigned int *)out;
1229 row2 = row1 + cols_2 + cols_2;
1230 lum2 = lum + cols_2 + cols_2;
1231 for (y=0; y<rows; y+=2) {
1232 for (x=0; x<cols_2; x++) {
1233 int R, G, B;
1234
1235 CR = *cr++;
1236 CB = *cb++;
1237 cr_r = Cr_r_tab[CR];
1238 cr_g = Cr_g_tab[CR];
1239 cb_g = Cb_g_tab[CB];
1240 cb_b = Cb_b_tab[CB];
1241
1242 L = L_tab[(int) *lum++];
1243
1244 R = L + cr_r;
1245 G = L + cr_g + cb_g;
1246 B = L + cb_b;
1247
1248 *row1++ = (r_2_pix[R] | g_2_pix[G] | b_2_pix[B]);
1249
1250 L = L_tab[(int) *lum++];
1251
1252 R = L + cr_r;
1253 G = L + cr_g + cb_g;
1254 B = L + cb_b;
1255
1256 *row1++ = (r_2_pix[R] | g_2_pix[G] | b_2_pix[B]);
1257
1258 /*
1259 * Now, do second row.
1260 */
1261
1262 L = L_tab [(int) *lum2++];
1263 R = L + cr_r;
1264 G = L + cr_g + cb_g;
1265 B = L + cb_b;
1266
1267 *row2++ = (r_2_pix[R] | g_2_pix[G] | b_2_pix[B]);
1268
1269 L = L_tab [(int) *lum2++];
1270 R = L + cr_r;
1271 G = L + cr_g + cb_g;
1272 B = L + cb_b;
1273
1274 *row2++ = (r_2_pix[R] | g_2_pix[G] | b_2_pix[B]);
1275 }
1276 lum += cols_2 + cols_2;
1277 lum2 += cols_2 + cols_2;
1278 row1 += cols_2 + cols_2;
1279 row2 += cols_2 + cols_2;
1280 }
1281}
1282
1283/* Array that remaps color numbers to actual pixel values used by X server. */
1284
1285unsigned char pixel[256];
1286
1287//extern char *strrchr();
1288#define PPM_BITS 8
1289
1290
1291/*
1292 *--------------------------------------------------------------
1293 *
1294 * ExecuteTexture --
1295 *
1296 * Write out a display plane as a OpenGL texture.
1297 *
1298 * Results:
1299 * None.
1300 *
1301 * Side effects:
1302 * None.
1303 *
1304 *--------------------------------------------------------------
1305 */
1306
1307
1308void
1309ExecuteTexture(vid_stream)
1311{
1312 unsigned int *p;
1313 unsigned int r, g, b;
1314 unsigned int i,j;
1315 int hsize;
1316 int tmpvv;
1317 int blockSize;
1318 char *tmpptr;
1319
1320 // v_size = height, h_size = width (vertical, horizontal)
1321 static GLubyte *Image = NULL;
1322 static int lastsize = 0;
1323 blockSize = sizeof(GLubyte) * 3 * vid_stream->v_size * vid_stream->h_size;
1324 if(Image == NULL || blockSize != lastsize)
1325 Image = realloc(Image,blockSize);
1326 //GLubyte Image[vid_stream->v_size][vid_stream->h_size][3];
1327
1328 // if the hsize is a power of 2.
1329 hsize = vid_stream->mb_width * 16;
1330
1331 for (i=0; i<vid_stream->v_size; i++) {
1332 p = (unsigned int *) vid_stream->current->display + i*hsize;
1333 for (j=0; j<vid_stream->h_size; j++){
1334
1335 r = *p & 0xff;
1336 g = (*p >> PPM_BITS) & 0xff;
1337 b = (*p >> (2*PPM_BITS)) & 0xff;
1338 //Image [vid_stream->v_size-i-1][j][0]=r;
1339 //Image [vid_stream->v_size-i-1][j][1]=g;
1340 //Image [vid_stream->v_size-i-1][j][2]=b;
1341 Image [((vid_stream->v_size-i-1)*vid_stream->h_size + j)*3 +0]=r;
1342 Image [((vid_stream->v_size-i-1)*vid_stream->h_size + j)*3 +1]=g;
1343 Image [((vid_stream->v_size-i-1)*vid_stream->h_size + j)*3 +2]=b;
1344 p++;
1345 }
1346 }
1347
1348 // store this frame for later binding in the correct thread
1349 *xSize= vid_stream->h_size;
1350 *ySize = vid_stream->v_size;
1351
1352 //blockSize = sizeof(GLubyte) * 3 * vid_stream->v_size * vid_stream->h_size;
1353
1354 dataPointer = realloc(dataPointer,blockSize * (*frameCount));
1355 tmpptr = dataPointer + (blockSize * ((*frameCount)-1));
1356
1357 memcpy (tmpptr, Image, blockSize);
1358 (*frameCount)++;
1359
1360}
1361
1362/* Bit masks used by bit i/o operations. */
1363unsigned int nBitMask[] = { 0x00000000, 0x80000000, 0xc0000000, 0xe0000000,
1364 0xf0000000, 0xf8000000, 0xfc000000, 0xfe000000,
1365 0xff000000, 0xff800000, 0xffc00000, 0xffe00000,
1366 0xfff00000, 0xfff80000, 0xfffc0000, 0xfffe0000,
1367 0xffff0000, 0xffff8000, 0xffffc000, 0xffffe000,
1368 0xfffff000, 0xfffff800, 0xfffffc00, 0xfffffe00,
1369 0xffffff00, 0xffffff80, 0xffffffc0, 0xffffffe0,
1370 0xfffffff0, 0xfffffff8, 0xfffffffc, 0xfffffffe};
1371
1372unsigned int bitMask[] = { 0xffffffff, 0x7fffffff, 0x3fffffff, 0x1fffffff,
1373 0x0fffffff, 0x07ffffff, 0x03ffffff, 0x01ffffff,
1374 0x00ffffff, 0x007fffff, 0x003fffff, 0x001fffff,
1375 0x000fffff, 0x0007ffff, 0x0003ffff, 0x0001ffff,
1376 0x0000ffff, 0x00007fff, 0x00003fff, 0x00001fff,
1377 0x00000fff, 0x000007ff, 0x000003ff, 0x000001ff,
1378 0x000000ff, 0x0000007f, 0x0000003f, 0x0000001f,
1379 0x0000000f, 0x00000007, 0x00000003, 0x00000001};
1380
1381unsigned int rBitMask[] = { 0xffffffff, 0xfffffffe, 0xfffffffc, 0xfffffff8,
1382 0xfffffff0, 0xffffffe0, 0xffffffc0, 0xffffff80,
1383 0xffffff00, 0xfffffe00, 0xfffffc00, 0xfffff800,
1384 0xfffff000, 0xffffe000, 0xffffc000, 0xffff8000,
1385 0xffff0000, 0xfffe0000, 0xfffc0000, 0xfff80000,
1386 0xfff00000, 0xffe00000, 0xffc00000, 0xff800000,
1387 0xff000000, 0xfe000000, 0xfc000000, 0xf8000000,
1388 0xf0000000, 0xe0000000, 0xc0000000, 0x80000000};
1389
1390unsigned int bitTest[] = { 0x80000000, 0x40000000, 0x20000000, 0x10000000,
1391 0x08000000, 0x04000000, 0x02000000, 0x01000000,
1392 0x00800000, 0x00400000, 0x00200000, 0x00100000,
1393 0x00080000, 0x00040000, 0x00020000, 0x00010000,
1394 0x00008000, 0x00004000, 0x00002000, 0x00001000,
1395 0x00000800, 0x00000400, 0x00000200, 0x00000100,
1396 0x00000080, 0x00000040, 0x00000020, 0x00000010,
1397 0x00000008, 0x00000004, 0x00000002, 0x00000001};
1398
1399
1400/*
1401 *--------------------------------------------------------------
1402 *
1403 * correct_underflow --
1404 *
1405 * Called when buffer does not have sufficient data to
1406 * satisfy request for bits.
1407 * Calls get_more_data, an application specific routine
1408 * required to fill the buffer with more data.
1409 *
1410 * Results:
1411 * None really.
1412 *
1413 * Side effects:
1414 * buf_length and buffer fields may be changed.
1415 *
1416 *--------------------------------------------------------------
1417 */
1418
1419void
1420correct_underflow(vid_stream)
1422{
1423
1424 int status;
1425
1426 status = get_more_data(vid_stream);
1427
1428 if (status < 0) {
1429 fprintf (stderr, "\n");
1430 perror("Unexpected read error.");
1431 }
1432 else if ((status == 0) && (vid_stream->buf_length < 1)) {
1433 printf("\nImproper or missing sequence end code.\n");
1434 vid_stream->film_has_ended=TRUE;
1435 clear_data_stream(vid_stream);
1436 return;
1437 }
1438#ifdef UTIL2
1439 vid_stream->curBits = *vid_stream->buffer << vid_stream->bit_offset;
1440#else
1441 vid_stream->curBits = *vid_stream->buffer;
1442#endif
1443
1444}
1445
1446
1447/*
1448 *--------------------------------------------------------------
1449 *
1450 * next_bits --
1451 *
1452 * Compares next num bits to low order position in mask.
1453 * Buffer pointer is NOT advanced.
1454 *
1455 * Results:
1456 * TRUE, FALSE, or error code.
1457 *
1458 * Side effects:
1459 * None.
1460 *
1461 *--------------------------------------------------------------
1462 */
1463
1464int next_bits(num, mask, vid_stream)
1465int num;
1466unsigned int mask;
1468{
1469 unsigned int stream;
1470 int ret_value;
1471
1472 /* If no current stream, return error. */
1473
1474 if (vid_stream == NULL)
1475 return NO_VID_STREAM;
1476
1477 /* Get next num bits, no buffer pointer advance. */
1478
1479 show_bitsn(num, stream);
1480
1481 /* Compare bit stream and mask. Set return value toTRUE if equal, FALSE if
1482 differs.
1483 */
1484
1485 if (mask == stream) {
1486 ret_value = TRUE;
1487 } else ret_value = FALSE;
1488
1489 /* Return return value. */
1490 return ret_value;
1491}
1492
1493
1494/*
1495 *--------------------------------------------------------------
1496 *
1497 * get_ext_data --
1498 *
1499 * Assumes that bit stream is at begining of extension
1500 * data. Parses off extension data into dynamically
1501 * allocated space until start code is hit.
1502 *
1503 * Results:
1504 * Pointer to dynamically allocated memory containing
1505 * extension data.
1506 *
1507 * Side effects:
1508 * Bit stream irreversibly parsed.
1509 *
1510 *--------------------------------------------------------------
1511 */
1512
1513char *get_ext_data (vid_stream)
1515{
1516 unsigned int size, marker;
1517 char *dataPtr;
1518 unsigned int data;
1519
1520 /* Set initial ext data buffer size. */
1521
1522 size = EXT_BUF_SIZE;
1523
1524 /* Allocate ext data buffer. */
1525
1526 dataPtr = (char *) malloc(size);
1527
1528 /* Initialize marker to keep place in ext data buffer. */
1529
1530 marker = 0;
1531
1532 /* While next data is not start code... */
1533 while (!next_bits(24, 0x000001, vid_stream)) {
1534
1535 /* Get next byte of ext data. */
1536
1537 get_bits8(data);
1538
1539 /* Put ext data into ext data buffer. Advance marker. */
1540
1541 dataPtr[marker] = (char) data;
1542 marker++;
1543
1544 /* If end of ext data buffer reached, resize data buffer. */
1545
1546 if (marker == size) {
1547 size += EXT_BUF_SIZE;
1548 dataPtr = (char *) realloc(dataPtr, size);
1549 }
1550 }
1551
1552 /* Realloc data buffer to free any extra space. */
1553
1554 dataPtr = (char *) realloc(dataPtr, marker);
1555
1556 /* Return pointer to ext data buffer. */
1557 return dataPtr;
1558}
1559
1560
1561/*
1562 *--------------------------------------------------------------
1563 *
1564 * next_start_code --
1565 *
1566 * Parses off bitstream until start code reached. When done
1567 * next 4 bytes of bitstream will be start code. Bit offset
1568 * reset to 0.
1569 *
1570 * Results:
1571 * Status code.
1572 *
1573 * Side effects:
1574 * Bit stream irreversibly parsed.
1575 *
1576 *--------------------------------------------------------------
1577 */
1578
1579int next_start_code(vid_stream)
1581{
1582 int state;
1583 int byteoff;
1584 unsigned int data;
1585
1586
1587 /* If no current stream, return error. */
1588
1589 if (vid_stream== NULL)
1590 return NO_VID_STREAM;
1591
1592 /* If insufficient buffer length, correct underflow. */
1593
1594 if (vid_stream->buf_length < 4) {
1595 correct_underflow(vid_stream);
1596 }
1597
1598 /* If bit offset not zero, reset and advance buffer pointer. */
1599
1600 byteoff = vid_stream->bit_offset % 8;
1601
1602 if (byteoff != 0) {
1603 flush_bits((8-byteoff));
1604 }
1605
1606 /* Set state = 0. */
1607
1608 state = 0;
1609
1610 /* While buffer has data ... */
1611
1612 while(vid_stream->buf_length > 0) {
1613
1614 /* If insufficient data exists, correct underflow. */
1615
1616
1617 if (vid_stream->buf_length < 4) {
1618 correct_underflow(vid_stream);
1619 }
1620
1621 /* If next byte is zero... */
1622
1623 get_bits8(data);
1624
1625 if (data == 0) {
1626
1627 /* If state < 2, advance state. */
1628
1629 if (state < 2) state++;
1630 }
1631
1632 /* If next byte is one... */
1633
1634 else if (data == 1) {
1635
1636 /* If state == 2, advance state (i.e. start code found). */
1637
1638 if (state == 2) state++;
1639
1640 /* Otherwise, reset state to zero. */
1641
1642 else state = 0;
1643 }
1644
1645 /* Otherwise byte is neither 1 or 0, reset state to 0. */
1646
1647 else {
1648 state = 0;
1649 }
1650
1651 /* If state == 3 (i.e. start code found)... */
1652
1653 if (state == 3) {
1654
1655 /* Set buffer pointer back and reset length & bit offsets so
1656 * next bytes will be beginning of start code.
1657 */
1658
1659 vid_stream->bit_offset = vid_stream->bit_offset - 24;
1660
1661 if (vid_stream->bit_offset < 0) {
1662 vid_stream->bit_offset = 32 + vid_stream->bit_offset;
1663 vid_stream->buf_length++;
1664 vid_stream->buffer--;
1665#ifdef UTIL2
1666 vid_stream->curBits = *vid_stream->buffer << vid_stream->bit_offset;
1667#else
1668 vid_stream->curBits = *vid_stream->buffer;
1669#endif
1670 }
1671 else {
1672#ifdef UTIL2
1673 vid_stream->curBits = *vid_stream->buffer << vid_stream->bit_offset;
1674#else
1675 vid_stream->curBits = *vid_stream->buffer;
1676#endif
1677 }
1678
1679 /* Return success. */
1680 return OK;
1681 }
1682 }
1683
1684 /* Return underflow error. */
1685 return STREAM_UNDERFLOW;
1686}
1687
1688
1689/*
1690 *--------------------------------------------------------------
1691 *
1692 * get_extra_bit_info --
1693 *
1694 * Parses off extra bit info stream into dynamically
1695 * allocated memory. Extra bit info is indicated by
1696 * a flag bit set to 1, followed by 8 bits of data.
1697 * This continues until the flag bit is zero. Assumes
1698 * that bit stream set to first flag bit in extra
1699 * bit info stream.
1700 *
1701 * Results:
1702 * Pointer to dynamically allocated memory with extra
1703 * bit info in it. Flag bits are NOT included.
1704 *
1705 * Side effects:
1706 * Bit stream irreversibly parsed.
1707 *
1708 *--------------------------------------------------------------
1709 */
1710
1711char *get_extra_bit_info(vid_stream)
1713{
1714 unsigned int size, marker;
1715 char *dataPtr;
1716 unsigned int data;
1717
1718 /* Get first flag bit. */
1719 get_bits1(data);
1720
1721 /* If flag is false, return NULL pointer (i.e. no extra bit info). */
1722
1723 if (!data) return NULL;
1724
1725 /* Initialize size of extra bit info buffer and allocate. */
1726
1727 size = EXT_BUF_SIZE;
1728 dataPtr = (char *) malloc(size);
1729
1730 /* Reset marker to hold place in buffer. */
1731
1732 marker = 0;
1733
1734 /* While flag bit is true. */
1735
1736 while (data) {
1737
1738 /* Get next 8 bits of data. */
1739 get_bits8(data);
1740
1741 /* Place in extra bit info buffer. */
1742
1743 dataPtr[marker] = (char) data;
1744 marker++;
1745
1746 /* If buffer is full, reallocate. */
1747
1748 if (marker == size) {
1749 size += EXT_BUF_SIZE;
1750 dataPtr = (char *) realloc(dataPtr, size);
1751 }
1752
1753 /* Get next flag bit. */
1754 get_bits1(data);
1755 }
1756
1757 /* Reallocate buffer to free extra space. */
1758 dataPtr = (char *) realloc(dataPtr, marker);
1759
1760 /* Return pointer to extra bit info buffer. */
1761 return dataPtr;
1762}
1763
1764#ifdef DCPREC
1765extern int dcprec;
1766#endif
1767
1768/* Macro for returning 1 if num is positive, -1 if negative, 0 if 0. */
1769
1770#define Sign(num) ((num > 0) ? 1 : ((num == 0) ? 0 : -1))
1771
1772
1773/*
1774 *--------------------------------------------------------------
1775 *
1776 * ParseReconBlock --
1777 *
1778 * Parse values for block structure from bitstream.
1779 * n is an indication of the position of the block within
1780 * the macroblock (i.e. 0-5) and indicates the type of
1781 * block (i.e. luminance or chrominance). Reconstructs
1782 * coefficients from values parsed and puts in
1783 * block.dct_recon array in vid stream structure.
1784 * sparseFlag is set when the block contains only one
1785 * coeffictient and is used by the IDCT.
1786 *
1787 * Results:
1788 *
1789 *
1790 * Side effects:
1791 * Bit stream irreversibly parsed.
1792 *
1793 *--------------------------------------------------------------
1794 */
1795
1796#define DCT_recon blockPtr->dct_recon
1797#define DCT_dc_y_past blockPtr->dct_dc_y_past
1798#define DCT_dc_cr_past blockPtr->dct_dc_cr_past
1799#define DCT_dc_cb_past blockPtr->dct_dc_cb_past
1800
1801#define DECODE_DCT_COEFF_FIRST DecodeDCTCoeffFirst
1802#define DECODE_DCT_COEFF_NEXT DecodeDCTCoeffNext
1803
1804void
1805ParseReconBlock(n, vid_stream)
1806 int n;
1808{
1809#ifdef RISC
1810 unsigned int temp_curBits;
1811 int temp_bitOffset;
1812 int temp_bufLength;
1813 unsigned int *temp_bitBuffer;
1814#endif
1815
1816 Block *blockPtr = &vid_stream->block;
1817 int coeffCount=0;
1818
1819 if (vid_stream->buf_length < 100)
1820 correct_underflow(vid_stream);
1821
1822#ifdef RISC
1823 temp_curBits = vid_stream->curBits;
1824 temp_bitOffset = vid_stream->buf_offset;
1825 temp_bufLength = vid_stream->buf_length;
1826 temp_bitBuffer = bitBuffer;
1827#endif
1828
1829 {
1830 /*
1831 * Copy the mpeg_VidStream fields curBits, bitOffset, and bitBuffer
1832 * into local variables with the same names, so the macros use the
1833 * local variables instead. This allows register allocation and
1834 * can provide 1-2 fps speedup. On machines with not so many registers,
1835 * don't do this.
1836 */
1837#ifdef RISC
1838 register unsigned int curBits = temp_curBits;
1839 register int bitOffset = temp_bitOffset;
1840 register int bufLength = temp_bufLength;
1841 register unsigned int *bitBuffer = temp_bitBuffer;
1842#endif
1843
1844 int diff;
1845 int size, level=0, i, run, pos, coeff;
1846 short int *reconptr;
1847 unsigned char *iqmatrixptr, *niqmatrixptr;
1848 int qscale;
1849
1850 reconptr = DCT_recon[0];
1851
1852 /*
1853 * Hand coded version of memset that's a little faster...
1854 * Old call:
1855 * memset((char *) DCT_recon, 0, 64*sizeof(short int));
1856 */
1857 {
1858 int *p;
1859 p = (int *) reconptr;
1860
1861 p[0] = p[1] = p[2] = p[3] = p[4] = p[5] = p[6] = p[7] = p[8] = p[9] =
1862 p[10] = p[11] = p[12] = p[13] = p[14] = p[15] = p[16] = p[17] = p[18] =
1863 p[19] = p[20] = p[21] = p[22] = p[23] = p[24] = p[25] = p[26] = p[27] =
1864 p[28] = p[29] = p[30] = p[31] = 0;
1865
1866 }
1867
1868 if (vid_stream->mblock.mb_intra) {
1869
1870 if (n < 4) {
1871
1872 /*
1873 * Get the luminance bits. This code has been hand optimized to
1874 * get by the normal bit parsing routines. We get some speedup
1875 * by grabbing the next 16 bits and parsing things locally.
1876 * Thus, calls are translated as:
1877 *
1878 * show_bitsX <--> next16bits >> (16-X)
1879 * get_bitsX <--> val = next16bits >> (16-flushed-X);
1880 * flushed += X;
1881 * next16bits &= bitMask[flushed];
1882 * flush_bitsX <--> flushed += X;
1883 * next16bits &= bitMask[flushed];
1884 *
1885 * I've streamlined the code a lot, so that we don't have to mask
1886 * out the low order bits and a few of the extra adds are removed.
1887 * bsmith
1888 */
1889 unsigned int next16bits, index, flushed;
1890
1891 show_bits16(next16bits);
1892 index = next16bits >> (16-5);
1893 if (index < 31) {
1894 size = dct_dc_size_luminance[index].value;
1895 flushed = dct_dc_size_luminance[index].num_bits;
1896 } else {
1897 index = next16bits >> (16-9);
1898 index -= 0x1f0;
1899 size = dct_dc_size_luminance1[index].value;
1900 flushed = dct_dc_size_luminance1[index].num_bits;
1901 }
1902 next16bits &= bitMask[16+flushed];
1903
1904 if (size != 0) {
1905 flushed += size;
1906 diff = next16bits >> (16-flushed);
1907 if (!(diff & bitTest[32-size])) {
1908 diff = rBitMask[size] | (diff + 1);
1909 }
1910 } else {
1911 diff = 0;
1912 }
1913 flush_bits(flushed);
1914
1915 if (n == 0) {
1916 coeff = diff << 3;
1917 if (vid_stream->mblock.mb_address -
1918 vid_stream->mblock.past_intra_addr > 1) {
1919 coeff += 1024;
1920 } else {
1921 coeff += DCT_dc_y_past;
1922 }
1923 DCT_dc_y_past = coeff;
1924 } else {
1925 coeff = DCT_dc_y_past + (diff << 3);
1926 DCT_dc_y_past = coeff;
1927 }
1928
1929 } else { /* n = 4 or 5 */
1930
1931 /*
1932 * Get the chrominance bits. This code has been hand optimized to
1933 * as described above
1934 */
1935
1936 unsigned int next16bits, index, flushed;
1937
1938 show_bits16(next16bits);
1939 index = next16bits >> (16-5);
1940 if (index < 31) {
1941 size = dct_dc_size_chrominance[index].value;
1942 flushed = dct_dc_size_chrominance[index].num_bits;
1943 } else {
1944 index = next16bits >> (16-10);
1945 index -= 0x3e0;
1946 size = dct_dc_size_chrominance1[index].value;
1947 flushed = dct_dc_size_chrominance1[index].num_bits;
1948 }
1949 next16bits &= bitMask[16+flushed];
1950
1951 if (size != 0) {
1952 flushed += size;
1953 diff = next16bits >> (16-flushed);
1954 if (!(diff & bitTest[32-size])) {
1955 diff = rBitMask[size] | (diff + 1);
1956 }
1957 } else {
1958 diff = 0;
1959 }
1960 flush_bits(flushed);
1961
1962 /* We test 5 first; a result of the mixup of Cr and Cb */
1963 if (n == 5) {
1964 coeff = diff << 3;
1965
1966 if (vid_stream->mblock.mb_address -
1967 vid_stream->mblock.past_intra_addr > 1) {
1968 coeff += 1024;
1969 } else {
1970 coeff += DCT_dc_cr_past;
1971 }
1972 DCT_dc_cr_past = coeff;
1973 } else {
1974 coeff = diff << 3;
1975 if (vid_stream->mblock.mb_address -
1976 vid_stream->mblock.past_intra_addr > 1) {
1977 coeff += 1024;
1978 } else {
1979 coeff += DCT_dc_cb_past;
1980 }
1981 DCT_dc_cb_past = coeff;
1982 }
1983 }
1984
1985 *reconptr = coeff;
1986 i = 0;
1987 pos = 0;
1988 coeffCount = (coeff != 0);
1989
1990 if (vid_stream->picture.code_type != 4) {
1991
1992 qscale = vid_stream->slice.quant_scale;
1993 iqmatrixptr = vid_stream->intra_quant_matrix[0];
1994
1995 while(1) {
1996
1997 DECODE_DCT_COEFF_NEXT(run, level);
1998
1999 if (run >= END_OF_BLOCK) break;
2000
2001 i = i + run + 1;
2002 pos = zigzag_direct[i];
2003
2004 /* quantizes and oddifies each coefficient */
2005 if (level < 0) {
2006 coeff = ((level<<1) * qscale *
2007 ((int) (iqmatrixptr[pos]))) / 16;
2008 coeff += (1 - (coeff & 1));
2009 } else {
2010 coeff = ((level<<1) * qscale *
2011 ((int) (*(iqmatrixptr+pos)))) >> 4;
2012 coeff -= (1 - (coeff & 1));
2013 }
2014 reconptr[pos] = coeff;
2015 coeffCount++;
2016
2017 }
2018 static int jcount = 0;
2019 jcount ++;
2020 printf(" %d ",jcount);
2021 flush_bits(2);
2022
2023 goto end;
2024 }
2025 } else { /* non-intra-coded macroblock */
2026
2027 niqmatrixptr = vid_stream->non_intra_quant_matrix[0];
2028 qscale = vid_stream->slice.quant_scale;
2029
2030 DECODE_DCT_COEFF_FIRST(run, level);
2031 i = run;
2032
2033 pos = zigzag_direct[i];
2034
2035 /* quantizes and oddifies each coefficient */
2036 if (level < 0) {
2037 coeff = (((level<<1) - 1) * qscale *
2038 ((int) (niqmatrixptr[pos]))) / 16;
2039 if ((coeff & 1) == 0) {coeff = coeff + 1;}
2040 } else {
2041 coeff = (((level<<1) + 1) * qscale *
2042 ((int) (*(niqmatrixptr+pos)))) >> 4;
2043 coeff = (coeff-1) | 1; /* equivalent to: if ((coeff&1)==0) coeff = coeff - 1; */
2044 }
2045
2046 reconptr[pos] = coeff;
2047 if (coeff) {
2048 coeffCount = 1;
2049 }
2050
2051 if (vid_stream->picture.code_type != 4) {
2052
2053 while(1) {
2054
2055 DECODE_DCT_COEFF_NEXT(run, level);
2056
2057 if (run >= END_OF_BLOCK) {
2058 break;
2059 }
2060
2061 i = i+run+1;
2062 pos = zigzag_direct[i];
2063 if (level < 0) {
2064 coeff = (((level<<1) - 1) * qscale *
2065 ((int) (niqmatrixptr[pos]))) / 16;
2066 if ((coeff & 1) == 0) {coeff = coeff + 1;}
2067 } else {
2068 coeff = (((level<<1) + 1) * qscale *
2069 ((int) (*(niqmatrixptr+pos)))) >> 4;
2070 coeff = (coeff-1) | 1; /* equivalent to: if ((coeff&1)==0) coeff = coeff - 1; */
2071 }
2072 reconptr[pos] = coeff;
2073 coeffCount++;
2074 } /* end while */
2075
2076 flush_bits(2);
2077
2078 goto end;
2079 } /* end if (vid_stream->picture.code_type != 4) */
2080 }
2081
2082 end:
2083
2084 if (coeffCount == 1) {
2085 j_rev_dct_sparse (reconptr, pos);
2086 }
2087 else {
2088#ifdef FLOATDCT
2089 if (qualityFlag) {
2090 float_idct(reconptr);
2091 } else {
2092#endif
2093 j_rev_dct(reconptr);
2094#ifdef FLOATDCT
2095 }
2096#endif
2097 }
2098
2099#ifdef RISC
2100 temp_curBits = vid_stream->curBits;
2101 temp_bitOffset = vid_stream->bit_offset;
2102 temp_bufLength = vid_stream->buf_length;
2103 temp_bitBuffer = bitBuffer;
2104#endif
2105
2106 }
2107
2108#ifdef RISC
2109 vid_stream->curBits = temp_curBits;
2110 vid_stream->bitOffset = temp_bitOffset;
2111 vid_stream->buf_length = temp_bufLength;
2112 bitBuffer = temp_bitBuffer;
2113#endif
2114}
2115
2116#undef DCT_recon
2117#undef DCT_dc_y_past
2118#undef DCT_dc_cr_past
2119#undef DCT_dc_cb_past
2120
2121
2122/*
2123 *--------------------------------------------------------------
2124 *
2125 * ParseAwayBlock --
2126 *
2127 * Parses off block values, throwing them away.
2128 * Used with grayscale dithering.
2129 *
2130 * Results:
2131 * None.
2132 *
2133 * Side effects:
2134 * None.
2135 *
2136 *--------------------------------------------------------------
2137 */
2138
2139void
2140ParseAwayBlock(n, vid_stream)
2141 int n;
2143{
2144 unsigned int diff;
2145 unsigned int size, run;
2146 int level;
2147
2148 if (vid_stream->buf_length < 100)
2149 correct_underflow(vid_stream);
2150
2151 if (vid_stream->mblock.mb_intra) {
2152
2153 /* If the block is a luminance block... */
2154
2155 if (n < 4) {
2156
2157 /* Parse and decode size of first coefficient. */
2158
2159 DecodeDCTDCSizeLum(size);
2160
2161 /* Parse first coefficient. */
2162
2163 if (size != 0) {
2164 get_bitsn(size, diff);
2165 }
2166 }
2167
2168 /* Otherwise, block is chrominance block... */
2169
2170 else {
2171
2172 /* Parse and decode size of first coefficient. */
2173
2174 DecodeDCTDCSizeChrom(size);
2175
2176 /* Parse first coefficient. */
2177
2178 if (size != 0) {
2179 get_bitsn(size, diff);
2180 }
2181 }
2182 }
2183
2184 /* Otherwise, block is not intracoded... */
2185
2186 else {
2187
2188 /* Decode and set first coefficient. */
2189
2190 DECODE_DCT_COEFF_FIRST(run, level);
2191 }
2192
2193 /* If picture is not D type (i.e. I, P, or B)... */
2194
2195 if (vid_stream->picture.code_type != 4) {
2196
2197 /* While end of macroblock has not been reached... */
2198
2199 while (1) {
2200
2201 /* Get the dct_coeff_next */
2202
2203 DECODE_DCT_COEFF_NEXT(run, level);
2204
2205 if (run >= END_OF_BLOCK) break;
2206 }
2207
2208 /* End_of_block */
2209
2210 flush_bits(2);
2211 }
2212}
2213/* Declarations of functions. */
2214static void ReconIMBlock();
2215static void ReconPMBlock();
2216static void ReconBMBlock();
2217static void ReconBiMBlock();
2218static void ReconSkippedBlock();
2219static void DoPictureDisplay();
2220static int ParseSeqHead();
2221static int ParseGOP();
2222static int ParsePicture();
2223static int ParseSlice();
2224static int ParseMacroBlock();
2225static void ProcessSkippedPFrameMBlocks();
2226static void ProcessSkippedBFrameMBlocks();
2227
2228/* Macro for returning 1 if num is positive, -1 if negative, 0 if 0. */
2229
2230#define Sign(num) ((num > 0) ? 1 : ((num == 0) ? 0 : -1))
2231
2232/* Set up array for fast conversion from zig zag order to row/column
2233 coordinates.
2234*/
2235
2236const int zigzag[64][2] = {
2237 {0, 0}, {1, 0}, {0, 1}, {0, 2}, {1, 1}, {2, 0}, {3, 0}, {2, 1},
2238 {1, 2}, {0, 3}, {0, 4}, {1, 3}, {2, 2}, {3, 1}, {4, 0}, {5, 0},
2239 {4, 1}, {3, 2}, {2, 3}, {1, 4}, {0, 5}, {0, 6}, {1, 5}, {2, 4},
2240 {3, 3}, {4, 2}, {5, 1}, {6, 0}, {7, 0}, {6, 1}, {5, 2}, {4, 3},
2241 {3, 4}, {2, 5}, {1, 6}, {0, 7}, {1, 7}, {2, 6}, {3, 5}, {4, 4},
2242 {5, 3}, {6, 2}, {7, 1}, {7, 2}, {6, 3}, {5, 4}, {4, 5}, {3, 6},
2243 {2, 7}, {3, 7}, {4, 6}, {5, 5}, {6, 4}, {7, 3}, {7, 4}, {6, 5},
2244 {5, 6}, {4, 7}, {5, 7}, {6, 6}, {7, 5}, {7, 6}, {6, 7}, {7, 7}};
2245/* Array mapping zigzag to array pointer offset. */
2246
2247const int zigzag_direct[64] = {
2248 0, 1, 8, 16, 9, 2, 3, 10, 17, 24, 32, 25, 18, 11, 4, 5, 12,
2249 19, 26, 33, 40, 48, 41, 34, 27, 20, 13, 6, 7, 14, 21, 28, 35,
2250 42, 49, 56, 57, 50, 43, 36, 29, 22, 15, 23, 30, 37, 44, 51,
2251 58, 59, 52, 45, 38, 31, 39, 46, 53, 60, 61, 54, 47, 55, 62, 63};
2252/* Set up array for fast conversion from row/column coordinates to
2253 zig zag order.
2254*/
2255
2256const int scan[8][8] = {
2257 {0, 1, 5, 6, 14, 15, 27, 28},
2258 {2, 4, 7, 13, 16, 26, 29, 42},
2259 {3, 8, 12, 17, 25, 30, 41, 43},
2260 {9, 11, 18, 24, 31, 40, 44, 53},
2261 {10, 19, 23, 32, 39, 45, 52, 54},
2262 {20, 22, 33, 38, 46, 51, 55, 60},
2263 {21, 34, 37, 47, 50, 56, 59, 61},
2264 {35, 36, 48, 49, 57, 58, 62, 63}};
2265/* Initialize P and B skip flags. */
2266
2267static int No_P_Flag = FALSE;
2268static int No_B_Flag = FALSE;
2269
2270/* Max lum, chrom indices for illegal block checking. */
2271
2272
2273/*
2274 * We use a lookup table to make sure values stay in the 0..255 range.
2275 * Since this is cropping (ie, x = (x < 0)?0:(x>255)?255:x; ), wee call this
2276 * table the "crop table".
2277 * MAX_NEG_CROP is the maximum neg/pos value we can handle.
2278 */
2279
2280#define MAX_NEG_CROP 2048
2281#define NUM_CROP_ENTRIES (2048+2*MAX_NEG_CROP)
2282static unsigned char cropTbl[NUM_CROP_ENTRIES];
2283
2284
2285
2286/*
2287 *--------------------------------------------------------------
2288 *
2289 * ReadSysClock --
2290 *
2291 * Computes the current time according to the system clock.
2292 *
2293 * Results:
2294 * The current time according to the system clock.
2295 *
2296 * Side effects:
2297 * None.
2298 *
2299 *--------------------------------------------------------------
2300 */
2301
2302
2303double
2304ReadSysClock()
2305{
2306 //struct timeval tv;
2307 //(void) gettimeofday(&tv, (struct timezone *)NULL);
2308 //return (tv.tv_sec + tv.tv_usec / 1000000.0);
2309 return TickTime();
2310}
2311
2312
2313/*
2314 *--------------------------------------------------------------
2315 *
2316 * InitCrop --
2317 *
2318 * Initializes cropTbl - this was taken from newmpeg_VidStream so
2319 * that it wasn't done for each new video stream
2320 *
2321 * Results:
2322 * None
2323 *
2324 * Side effects:
2325 * cropTbl will be initialized
2326 *
2327 *--------------------------------------------------------------
2328 */
2329void
2330InitCrop()
2331{
2332 int i;
2333
2334 /* Initialize crop table. */
2335
2336 for (i = (-MAX_NEG_CROP); i < NUM_CROP_ENTRIES - MAX_NEG_CROP; i++) {
2337 if (i <= 0) {
2338 cropTbl[i + MAX_NEG_CROP] = 0;
2339#ifdef TWELVE_BITS
2340 } else if (i >= 2047) {
2341 cropTbl[i + MAX_NEG_CROP] = 2047;
2342#endif
2343 } else if (i >= 255) {
2344 cropTbl[i + MAX_NEG_CROP] = 255;
2345 } else {
2346 cropTbl[i + MAX_NEG_CROP] = i;
2347 }
2348 }
2349
2350}
2351
2352
2353
2354/*
2355 *--------------------------------------------------------------
2356 *
2357 * mpg_NewVidStream --
2358 *
2359 * Allocates and initializes a mpeg_VidStream structure. Takes
2360 * as parameter requested size for buffer length.
2361 *
2362 * Results:
2363 * A pointer to the new mpeg_VidStream structure.
2364 *
2365 * Side effects:
2366 * None.
2367 *
2368 *--------------------------------------------------------------
2369 */
2370
2372mpg_NewVidStream(buffer_len)
2373 unsigned int buffer_len;
2374{
2375 int i, j;
2376 mpeg_VidStream *new;
2377 static const unsigned char default_intra_matrix[64] = {
2378 8, 16, 19, 22, 26, 27, 29, 34,
2379 16, 16, 22, 24, 27, 29, 34, 37,
2380 19, 22, 26, 27, 29, 34, 34, 38,
2381 22, 22, 26, 27, 29, 34, 37, 40,
2382 22, 26, 27, 29, 32, 35, 40, 48,
2383 26, 27, 29, 32, 35, 40, 48, 58,
2384 26, 27, 29, 34, 38, 46, 56, 69,
2385 27, 29, 35, 38, 46, 56, 69, 83};
2386
2387 /* Check for legal buffer length. */
2388
2389 if (buffer_len < 4)
2390 return NULL;
2391
2392 /* Make buffer length multiple of 4. */
2393
2394 buffer_len = (buffer_len + 3) >> 2;
2395
2396 /* Allocate memory for new structure. */
2397
2398 new = (mpeg_VidStream *) malloc(sizeof(mpeg_VidStream));
2399
2400 /* Initialize pointers to extension and user data. */
2401
2402 new->group.ext_data = NULL;
2403 new->group.user_data = NULL;
2404 new->picture.extra_info = NULL;
2405 new->picture.user_data = NULL;
2406 new->picture.ext_data = NULL;
2407 new->slice.extra_info = NULL;
2408 new->ext_data = NULL;
2409 new->user_data = NULL;
2410
2411 /* Copy default intra matrix. */
2412
2413 for (i = 0; i < 8; i++) {
2414 for (j = 0; j < 8; j++) {
2415 new->intra_quant_matrix[i][j] = default_intra_matrix[i * 8 + j];
2416 }
2417 }
2418
2419 /* Initialize non intra quantization matrix. */
2420
2421 for (i = 0; i < 8; i++) {
2422 for (j = 0; j < 8; j++) {
2423 new->non_intra_quant_matrix[i][j] = 16;
2424 }
2425 }
2426
2427 /* Initialize pointers to image spaces. */
2428
2429 new->current = new->past = new->future = NULL;
2430 for (i = 0; i < RING_BUF_SIZE; i++) {
2431 new->ring[i] = NULL;
2432 }
2433
2434 /* Create buffer. */
2435
2436
2437 new->buf_start = (unsigned int *) malloc(buffer_len * 4);
2438
2439 /*
2440 * Set max_buf_length to one less than actual length to deal with messy
2441 * data without proper seq. end codes.
2442 */
2443
2444 new->max_buf_length = buffer_len - 1;
2445
2446 /* Initialize bitstream i/o fields. */
2447
2448 new->bit_offset = 0;
2449 new->buf_length = 0;
2450 new->buffer = new->buf_start;
2451
2452 /* Initialize fields that used to be global */
2453 new->film_has_ended = FALSE;
2454 new->filename = NULL;
2455 new->EOF_flag = FALSE;
2456
2457 /* Return structure. */
2458
2459 return new;
2460}
2461
2462
2463
2464/*
2465 *--------------------------------------------------------------
2466 *
2467 * Destroympeg_VidStream --
2468 *
2469 * Deallocates a mpeg_VidStream structure.
2470 *
2471 * Results:
2472 * None.
2473 *
2474 * Side effects:
2475 * None.
2476 *
2477 *--------------------------------------------------------------
2478 */
2479void
2480Destroympeg_VidStream(astream)
2481 mpeg_VidStream *astream;
2482{
2483 int i;
2484
2485 if (astream->ext_data != NULL)
2486 free(astream->ext_data);
2487
2488 if (astream->user_data != NULL)
2489 free(astream->user_data);
2490
2491 if (astream->group.ext_data != NULL)
2492 free(astream->group.ext_data);
2493
2494 if (astream->group.user_data != NULL)
2495 free(astream->group.user_data);
2496
2497 if (astream->picture.extra_info != NULL)
2498 free(astream->picture.extra_info);
2499
2500 if (astream->picture.ext_data != NULL)
2501 free(astream->picture.ext_data);
2502
2503 if (astream->picture.user_data != NULL)
2504 free(astream->picture.user_data);
2505
2506 if (astream->slice.extra_info != NULL)
2507 free(astream->slice.extra_info);
2508
2509 if (astream->buf_start != NULL)
2510 free(astream->buf_start);
2511
2512 for (i = 0; i < RING_BUF_SIZE; i++) {
2513 if (astream->ring[i] != NULL) {
2514 DestroyPictImage(astream->ring[i]);
2515 astream->ring[i] = NULL;
2516 }
2517 }
2518
2519 free((char *) astream);
2520
2521}
2522
2523
2524
2525
2526/*
2527 *--------------------------------------------------------------
2528 *
2529 * NewPictImage --
2530 *
2531 * Allocates and initializes a PictImage structure.
2532 * The width and height of the image space are passed in
2533 * as parameters.
2534 *
2535 * Results:
2536 * A pointer to the new PictImage structure.
2537 *
2538 * Side effects:
2539 * None.
2540 *
2541 *--------------------------------------------------------------
2542 */
2543
2544PictImage *
2545NewPictImage(vid_stream)
2547{
2548 PictImage *new;
2549 unsigned int width=vid_stream->mb_width * 16;
2550 unsigned int height=vid_stream->mb_height * 16;
2551
2552 /* Allocate memory space for new structure. */
2553
2554 new = (PictImage *) malloc(sizeof(PictImage));
2555
2556
2557 /* Allocate memory for image spaces. */
2558
2559 {
2560 int temp_sz;
2561 int factor;
2562 temp_sz = vid_stream->matched_depth >> 3;
2563 if(!temp_sz) temp_sz = 1;
2564 if(temp_sz == 3) temp_sz = 4;
2565 factor = 1;
2566
2567 new->display = (unsigned char *) malloc(width * height * temp_sz *
2568 factor * factor);
2569 }
2570 new->luminance = (unsigned char *) malloc(width * height);
2571 new->Cr = (unsigned char *) malloc(width * height / 4);
2572 new->Cb = (unsigned char *) malloc(width * height / 4);
2573
2574 /* Reset locked flag. */
2575
2576 new->locked = 0;
2577
2578 /* Return pointer to new structure. */
2579
2580 return new;
2581}
2582
2583
2584
2585/*
2586 *--------------------------------------------------------------
2587 *
2588 * DestroyPictImage --
2589 *
2590 * Deallocates a PictImage structure.
2591 *
2592 * Results:
2593 * None.
2594 *
2595 * Side effects:
2596 * None.
2597 *
2598 *--------------------------------------------------------------
2599 */
2600void
2601DestroyPictImage(apictimage)
2602 PictImage *apictimage;
2603{
2604 if (apictimage->luminance != NULL) {
2605 free(apictimage->luminance);
2606 }
2607 if (apictimage->Cr != NULL) {
2608 free(apictimage->Cr);
2609 }
2610 if (apictimage->Cb != NULL) {
2611 free(apictimage->Cb);
2612 }
2613 if (apictimage->display != NULL) {
2614 free(apictimage->display);
2615 }
2616 free(apictimage);
2617}
2618
2619
2620/*
2621 *--------------------------------------------------------------
2622 *
2623 * mpegVidRsrc --
2624 *
2625 * Parses bit stream until MB_QUANTUM number of
2626 * macroblocks have been decoded or current slice or
2627 * picture ends, whichever comes first. If the start
2628 * of a frame is encountered, the frame is time stamped
2629 * with the value passed in time_stamp. If the value
2630 * passed in buffer is not null, the video stream buffer
2631 * is set to buffer and the length of the buffer is
2632 * expected in value passed in through length. The current
2633 * video stream is set to vid_stream. If vid_stream
2634 * is passed as NULL, a new mpeg_VidStream structure is created
2635 * and initialized and used as the current video stream.
2636 *
2637 * Results:
2638 * A pointer to the video stream structure used.
2639 *
2640 * Side effects:
2641 * Bit stream is irreversibly parsed. If a picture is completed,
2642 * a function is called to display the frame at the correct time.
2643 *
2644 *--------------------------------------------------------------
2645 */
2646
2648mpegVidRsrc(time_stamp, vid_stream, first)
2649 TimeStamp time_stamp;
2651 int first;
2652{
2653 unsigned int data;
2654 int i, status;
2655
2656 /* If vid_stream is null, create new mpeg_VidStream structure. */
2657
2658 if (vid_stream == NULL) {
2659 return NULL;
2660 }
2661
2662 /*
2663 * If called for the first time, find start code, make sure it is a
2664 * sequence start code.
2665 */
2666
2667 if (first) {
2668 vid_stream->sys_layer=-1;
2669 vid_stream->num_left=0;
2670 vid_stream->leftover_bytes=0;
2671 vid_stream->Parse_done=FALSE;
2672
2673 next_start_code(vid_stream); /* sets curBits */
2674
2675 show_bits32(data);
2676 if (data != SEQ_START_CODE) {
2677 printf("This is not an MPEG video stream. (%x)\n",data);
2678 Destroympeg_VidStream(vid_stream);
2679 return NULL;
2680 }
2681 } else {
2682#ifdef UTIL2
2683 vid_stream->curBits = *vid_stream->buffer << vid_stream->bit_offset;
2684#else
2685 vid_stream->curBits = *vid_stream->buffer;
2686#endif
2687 }
2688
2689 /* Get next 32 bits (size of start codes). */
2690 show_bits32(data);
2691
2692 /*
2693 * Process according to start code (or parse macroblock if not a start code
2694 * at all).
2695 */
2696
2697 switch (data) {
2698
2699 case SEQ_END_CODE:
2700 case 0x000001b9: /* handle ISO_11172_END_CODE too */
2701
2702 /* Display last frame. */
2703
2704 if (vid_stream->future != NULL) {
2705 vid_stream->current = vid_stream->future;
2706 }
2707
2708 vid_stream->film_has_ended=TRUE;
2709 Destroympeg_VidStream(vid_stream);
2710 vid_stream=NULL;
2711 goto done;
2712 break;
2713
2714 case SEQ_START_CODE:
2715 /* Sequence start code. Parse sequence header. */
2716
2717 if (ParseSeqHead(vid_stream) != PARSE_OK)
2718 goto error;
2719 goto done;
2720
2721 case GOP_START_CODE:
2722 /* Group of Pictures start code. Parse gop header. */
2723
2724 if (ParseGOP(vid_stream) != PARSE_OK)
2725 goto error;
2726 goto done;
2727
2728 case PICTURE_START_CODE:
2729 /* Picture start code. Parse picture header and first slice header. */
2730
2731 status = ParsePicture(vid_stream, time_stamp);
2732
2733 if (status == SKIP_PICTURE) {
2734 next_start_code(vid_stream);
2735 while (!next_bits(32, PICTURE_START_CODE, vid_stream)) {
2736 if (next_bits(32, GOP_START_CODE, vid_stream))
2737 break;
2738 else if (next_bits(32, SEQ_END_CODE, vid_stream))
2739 break;
2740 flush_bits(24);
2741 next_start_code(vid_stream);
2742 }
2743 goto done;
2744 } else if (status != PARSE_OK)
2745 goto error;
2746
2747
2748 if (ParseSlice(vid_stream) != PARSE_OK)
2749 goto error;
2750 break;
2751
2752 case SEQUENCE_ERROR_CODE:
2753 flush_bits32;
2754 next_start_code(vid_stream);
2755 goto done;
2756
2757 default:
2758
2759 /* Check for slice start code. */
2760
2761 if ((data >= SLICE_MIN_START_CODE) && (data <= SLICE_MAX_START_CODE)) {
2762
2763 /* Slice start code. Parse slice header. */
2764
2765 if (ParseSlice(vid_stream) != PARSE_OK)
2766 goto error;
2767 }
2768 break;
2769 }
2770
2771 /* Parse next MB_QUANTUM macroblocks. */
2772
2773 for (i = 0; i < MB_QUANTUM; i++) {
2774
2775 /* Check to see if actually a startcode and not a macroblock. */
2776
2777 if (!next_bits(23, 0x00000000, vid_stream)) {
2778
2779 /* Not start code. Parse Macroblock. */
2780
2781 if (ParseMacroBlock(vid_stream) != PARSE_OK)
2782 goto error;
2783
2784 } else {
2785
2786 /* Not macroblock, actually start code. Get start code. */
2787
2788 next_start_code(vid_stream);
2789 show_bits32(data);
2790
2791 /*
2792 * If start code is outside range of slice start codes, frame is
2793 * complete, display frame.
2794 */
2795
2796 if (((data < SLICE_MIN_START_CODE) || (data > SLICE_MAX_START_CODE)) &&
2797 (data != SEQUENCE_ERROR_CODE)) {
2798
2799 DoPictureDisplay(vid_stream);
2800 }
2801 goto done;
2802 }
2803 }
2804
2805 /* Check if we just finished a picture on the MB_QUANTUMth macroblock */
2806 if (next_bits(23, 0x00000000, vid_stream)) {
2807 next_start_code(vid_stream);
2808 show_bits32(data);
2809 if ((data < SLICE_MIN_START_CODE) || (data > SLICE_MAX_START_CODE)) {
2810 DoPictureDisplay(vid_stream);
2811 }
2812 }
2813
2814 /* Return pointer to video stream structure. */
2815
2816 goto done;
2817
2818error:
2819 vid_stream->film_has_ended=TRUE;
2820 Destroympeg_VidStream(vid_stream);
2821 goto done;
2822
2823done:
2824 return vid_stream;
2825}
2826
2827
2828
2829
2830/*
2831 *--------------------------------------------------------------
2832 *
2833 * ParseSeqHead --
2834 *
2835 * Assumes bit stream is at the begining of the sequence
2836 * header start code. Parses off the sequence header.
2837 *
2838 * Results:
2839 * Fills the vid_stream structure with values derived and
2840 * decoded from the sequence header. Allocates the pict image
2841 * structures based on the dimensions of the image space
2842 * found in the sequence header.
2843 *
2844 * Side effects:
2845 * Bit stream irreversibly parsed off.
2846 *
2847 *--------------------------------------------------------------
2848 */
2849static int
2850ParseSeqHead(vid_stream)
2852{
2853 unsigned int data;
2854 int i;
2855
2856 /* Flush off sequence start code. */
2857
2858 flush_bits32;
2859
2860 /* Get horizontal size of image space. */
2861
2862 get_bits12(data);
2863 vid_stream->h_size = data;
2864
2865 /* Get vertical size of image space. */
2866
2867 get_bits12(data);
2868 vid_stream->v_size = data;
2869
2870 /* Calculate macroblock width and height of image space. */
2871
2872 vid_stream->mb_width = (vid_stream->h_size + 15) / 16;
2873 vid_stream->mb_height = (vid_stream->v_size + 15) / 16;
2874
2875 /*
2876 * Initialize ring buffer of pict images now that dimensions of image space
2877 * are known.
2878 */
2879
2880 if (vid_stream->ring[0] == NULL) {
2881 for (i = 0; i < RING_BUF_SIZE; i++) {
2882 vid_stream->ring[i] = NewPictImage(vid_stream);
2883 }
2884 }
2885
2886 /* Parse of aspect ratio code. */
2887
2888 get_bits4(data);
2889 vid_stream->aspect_ratio = (unsigned char) data;
2890
2891 /* Parse off picture rate code. */
2892
2893 get_bits4(data);
2894 vid_stream->picture_rate = (unsigned char) data;
2895
2896 /* Parse off bit rate. */
2897
2898 get_bits18(data);
2899 vid_stream->bit_rate = data;
2900
2901 /* Flush marker bit. */
2902
2903 flush_bits(1);
2904
2905 /* Parse off vbv buffer size. */
2906
2907 get_bits10(data);
2908 vid_stream->vbv_buffer_size = data;
2909
2910 /* Parse off contrained parameter flag. */
2911
2912 get_bits1(data);
2913 if (data) {
2914 vid_stream->const_param_flag = TRUE;
2915 } else
2916 vid_stream->const_param_flag = FALSE;
2917
2918 /*
2919 * If intra_quant_matrix_flag set, parse off intra quant matrix values.
2920 */
2921
2922 get_bits1(data);
2923 if (data) {
2924 for (i = 0; i < 64; i++) {
2925 get_bits8(data);
2926
2927 vid_stream->intra_quant_matrix[zigzag[i][1]][zigzag[i][0]] =
2928 (unsigned char) data;
2929 }
2930 }
2931 /*
2932 * If non intra quant matrix flag set, parse off non intra quant matrix
2933 * values.
2934 */
2935
2936 get_bits1(data);
2937 if (data) {
2938 for (i = 0; i < 64; i++) {
2939 get_bits8(data);
2940
2941 vid_stream->non_intra_quant_matrix[zigzag[i][1]][zigzag[i][0]] =
2942 (unsigned char) data;
2943 }
2944 }
2945 /* Go to next start code. */
2946
2947 next_start_code(vid_stream);
2948
2949 /*
2950 * If next start code is extension start code, parse off extension data.
2951 */
2952
2953 if (next_bits(32, EXT_START_CODE, vid_stream)) {
2954 flush_bits32;
2955 if (vid_stream->ext_data != NULL) {
2956 free(vid_stream->ext_data);
2957 vid_stream->ext_data = NULL;
2958 }
2959 vid_stream->ext_data = get_ext_data(vid_stream);
2960 }
2961 /* If next start code is user start code, parse off user data. */
2962
2963 if (next_bits(32, USER_START_CODE, vid_stream)) {
2964 flush_bits32;
2965 if (vid_stream->user_data != NULL) {
2966 free(vid_stream->user_data);
2967 vid_stream->user_data = NULL;
2968 }
2969 vid_stream->user_data = get_ext_data(vid_stream);
2970 }
2971 return PARSE_OK;
2972}
2973
2974
2975
2976/*
2977 *--------------------------------------------------------------
2978 *
2979 * ParseGOP --
2980 *
2981 * Parses of group of pictures header from bit stream
2982 * associated with vid_stream.
2983 *
2984 * Results:
2985 * Values in gop header placed into video stream structure.
2986 *
2987 * Side effects:
2988 * Bit stream irreversibly parsed.
2989 *
2990 *--------------------------------------------------------------
2991 */
2992
2993static int
2994ParseGOP(vid_stream)
2996{
2997 unsigned int data;
2998
2999 /* Flush group of pictures start code. */
3000
3001 flush_bits32;
3002
3003 /* Parse off drop frame flag. */
3004
3005 get_bits1(data);
3006 if (data) {
3007 vid_stream->group.drop_flag = TRUE;
3008 } else
3009 vid_stream->group.drop_flag = FALSE;
3010
3011 /* Parse off hour component of time code. */
3012
3013 get_bits5(data);
3014 vid_stream->group.tc_hours = data;
3015
3016 /* Parse off minute component of time code. */
3017
3018 get_bits6(data);
3019 vid_stream->group.tc_minutes = data;
3020
3021 /* Flush marker bit. */
3022
3023 flush_bits(1);
3024
3025 /* Parse off second component of time code. */
3026
3027 get_bits6(data);
3028 vid_stream->group.tc_seconds = data;
3029
3030 /* Parse off picture count component of time code. */
3031
3032 get_bits6(data);
3033 vid_stream->group.tc_pictures = data;
3034
3035 /* Parse off closed gop and broken link flags. */
3036
3037 get_bits2(data);
3038 if (data > 1) {
3039 vid_stream->group.closed_gop = TRUE;
3040 if (data > 2) {
3041 vid_stream->group.broken_link = TRUE;
3042 } else
3043 vid_stream->group.broken_link = FALSE;
3044 } else {
3045 vid_stream->group.closed_gop = FALSE;
3046 if (data) {
3047 vid_stream->group.broken_link = TRUE;
3048 } else
3049 vid_stream->group.broken_link = FALSE;
3050 }
3051
3052 /* Goto next start code. */
3053
3054 next_start_code(vid_stream);
3055
3056 /* If next start code is extension data, parse off extension data. */
3057
3058 if (next_bits(32, EXT_START_CODE, vid_stream)) {
3059 flush_bits32;
3060 if (vid_stream->group.ext_data != NULL) {
3061 free(vid_stream->group.ext_data);
3062 vid_stream->group.ext_data = NULL;
3063 }
3064 vid_stream->group.ext_data = get_ext_data(vid_stream);
3065 }
3066 /* If next start code is user data, parse off user data. */
3067
3068 if (next_bits(32, USER_START_CODE,vid_stream)) {
3069 flush_bits32;
3070 if (vid_stream->group.user_data != NULL) {
3071 free(vid_stream->group.user_data);
3072 vid_stream->group.user_data = NULL;
3073 }
3074 vid_stream->group.user_data = get_ext_data(vid_stream);
3075 }
3076 return PARSE_OK;
3077}
3078
3079
3080
3081/*
3082 *--------------------------------------------------------------
3083 *
3084 * ParsePicture --
3085 *
3086 * Parses picture header. Marks picture to be presented
3087 * at particular time given a time stamp.
3088 *
3089 * Results:
3090 * Values from picture header put into video stream structure.
3091 *
3092 * Side effects:
3093 * Bit stream irreversibly parsed.
3094 *
3095 *--------------------------------------------------------------
3096 */
3097
3098static int
3099ParsePicture(vid_stream, time_stamp)
3101 TimeStamp time_stamp;
3102{
3103 unsigned int data;
3104 int i;
3105
3106 /* Flush header start code. */
3107 flush_bits32;
3108
3109 /* Parse off temporal reference. */
3110 get_bits10(data);
3111 vid_stream->picture.temp_ref = data;
3112
3113 /* Parse of picture type. */
3114 get_bits3(data);
3115 vid_stream->picture.code_type = data;
3116
3117 if ((vid_stream->picture.code_type == B_TYPE) &&
3118 (/* No_B_Flag || */
3119 (vid_stream->future == NULL) ||
3120 ((vid_stream->past == NULL) && !(vid_stream->group.closed_gop))))
3121 /* According to 2-D.5.1 (p D-18) this is ok, if the refereneces are OK */
3122 return SKIP_PICTURE;
3123
3124 if ((vid_stream->picture.code_type == P_TYPE) &&
3125 (No_P_Flag || (vid_stream->future == NULL)))
3126 return SKIP_PICTURE;
3127
3128 /* Parse off vbv buffer delay value. */
3129 get_bits16(data);
3130 vid_stream->picture.vbv_delay = data;
3131
3132 /* If P or B type frame... */
3133
3134 if ((vid_stream->picture.code_type == P_TYPE) ||
3135 (vid_stream->picture.code_type == B_TYPE)) {
3136
3137 /* Parse off forward vector full pixel flag. */
3138 get_bits1(data);
3139 if (data) {
3140 vid_stream->picture.full_pel_forw_vector = TRUE;
3141 } else {
3142 vid_stream->picture.full_pel_forw_vector = FALSE;
3143 }
3144
3145 /* Parse of forw_r_code. */
3146 get_bits3(data);
3147
3148 /* Decode forw_r_code into forw_r_size and forw_f. */
3149
3150 vid_stream->picture.forw_r_size = data - 1;
3151 vid_stream->picture.forw_f = (1 << vid_stream->picture.forw_r_size);
3152 }
3153 /* If B type frame... */
3154
3155 if (vid_stream->picture.code_type == B_TYPE) {
3156
3157 /* Parse off back vector full pixel flag. */
3158 get_bits1(data);
3159 if (data)
3160 vid_stream->picture.full_pel_back_vector = TRUE;
3161 else
3162 vid_stream->picture.full_pel_back_vector = FALSE;
3163
3164 /* Parse off back_r_code. */
3165 get_bits3(data);
3166
3167 /* Decode back_r_code into back_r_size and back_f. */
3168
3169 vid_stream->picture.back_r_size = data - 1;
3170 vid_stream->picture.back_f = (1 << vid_stream->picture.back_r_size);
3171 }
3172 /* Get extra bit picture info. */
3173
3174 if (vid_stream->picture.extra_info != NULL) {
3175 free(vid_stream->picture.extra_info);
3176 vid_stream->picture.extra_info = NULL;
3177 }
3178 vid_stream->picture.extra_info = get_extra_bit_info(vid_stream);
3179
3180 /* Goto next start code. */
3181 next_start_code(vid_stream);
3182
3183 /* If start code is extension start code, parse off extension data. */
3184
3185 if (next_bits(32, EXT_START_CODE, vid_stream)) {
3186 flush_bits32;
3187
3188 if (vid_stream->picture.ext_data != NULL) {
3189 free(vid_stream->picture.ext_data);
3190 vid_stream->picture.ext_data = NULL;
3191 }
3192 vid_stream->picture.ext_data = get_ext_data(vid_stream);
3193 }
3194 /* If start code is user start code, parse off user data. */
3195
3196 if (next_bits(32, USER_START_CODE, vid_stream)) {
3197 flush_bits32;
3198
3199 if (vid_stream->picture.user_data != NULL) {
3200 free(vid_stream->picture.user_data);
3201 vid_stream->picture.user_data = NULL;
3202 }
3203 vid_stream->picture.user_data = get_ext_data(vid_stream);
3204 }
3205 /* Find a pict image structure in ring buffer not currently locked. */
3206
3207 i = 0;
3208
3209 while (vid_stream->ring[i]->locked != 0) {
3210 if (++i >= RING_BUF_SIZE) {
3211 perror("Fatal error. Ring buffer full.");
3212 exit(1);
3213 }
3214 }
3215
3216 /* Set current pict image structure to the one just found in ring. */
3217
3218 vid_stream->current = vid_stream->ring[i];
3219
3220 /* Set time stamp. */
3221
3222 vid_stream->current->show_time = time_stamp;
3223
3224 /* Reset past macroblock address field. */
3225
3226 vid_stream->mblock.past_mb_addr = -1;
3227
3228 return PARSE_OK;
3229}
3230
3231
3232
3233/*
3234 *--------------------------------------------------------------
3235 *
3236 * ParseSlice --
3237 *
3238 * Parses off slice header.
3239 *
3240 * Results:
3241 * Values found in slice header put into video stream structure.
3242 *
3243 * Side effects:
3244 * Bit stream irreversibly parsed.
3245 *
3246 *--------------------------------------------------------------
3247 */
3248
3249static int
3250ParseSlice(vid_stream)
3252{
3253 unsigned int data;
3254
3255 /* Flush slice start code. */
3256
3257 flush_bits(24);
3258
3259 /* Parse off slice vertical position. */
3260
3261 get_bits8(data);
3262 vid_stream->slice.vert_pos = data;
3263
3264 /* Parse off quantization scale. */
3265
3266 get_bits5(data);
3267 vid_stream->slice.quant_scale = data;
3268
3269 /* Parse off extra bit slice info. */
3270
3271 if (vid_stream->slice.extra_info != NULL) {
3272 free(vid_stream->slice.extra_info);
3273 vid_stream->slice.extra_info = NULL;
3274 }
3275 vid_stream->slice.extra_info = get_extra_bit_info(vid_stream);
3276
3277 /* Reset past intrablock address. */
3278
3279 vid_stream->mblock.past_intra_addr = -2;
3280
3281 /* Reset previous recon motion vectors. */
3282
3283 vid_stream->mblock.recon_right_for_prev = 0;
3284 vid_stream->mblock.recon_down_for_prev = 0;
3285 vid_stream->mblock.recon_right_back_prev = 0;
3286 vid_stream->mblock.recon_down_back_prev = 0;
3287
3288 /* Reset macroblock address. */
3289
3290 vid_stream->mblock.mb_address = ((vid_stream->slice.vert_pos - 1) *
3291 vid_stream->mb_width) - 1;
3292
3293 /* Reset past dct dc y, cr, and cb values. */
3294
3295 vid_stream->block.dct_dc_y_past = 1024 << 3;
3296 vid_stream->block.dct_dc_cr_past = 1024 << 3;
3297 vid_stream->block.dct_dc_cb_past = 1024 << 3;
3298
3299 return PARSE_OK;
3300}
3301
3302
3303
3304/*
3305 *--------------------------------------------------------------
3306 *
3307 * ParseMacroBlock --
3308 *
3309 * Parseoff macroblock. Reconstructs DCT values. Applies
3310 * inverse DCT, reconstructs motion vectors, calculates and
3311 * set pixel values for macroblock in current pict image
3312 * structure.
3313 *
3314 * Results:
3315 * Here's where everything really happens. Welcome to the
3316 * heart of darkness.
3317 *
3318 * Side effects:
3319 * Bit stream irreversibly parsed off.
3320 *
3321 *--------------------------------------------------------------
3322 */
3323
3324static int ParseMacroBlock(mpeg_VidStream *vid_stream)
3325{
3326 int addr_incr;
3327 unsigned int data;
3328 int mask, i, recon_right_for, recon_down_for, recon_right_back,
3329 recon_down_back;
3330 int zero_block_flag;
3331 int mb_quant = 0, mb_motion_forw = 0, mb_motion_back = 0,
3332 mb_pattern = 0;
3333
3334 /*
3335 * Parse off macroblock address increment and add to macroblock address.
3336 */
3337 do {
3338 unsigned int ind;
3339 show_bits11(ind);
3340 DecodeMBAddrInc(addr_incr);
3341 if (mb_addr_inc[ind].num_bits==0) {
3342 addr_incr = 1;
3343 }
3344 if (addr_incr == MB_ESCAPE) {
3345 vid_stream->mblock.mb_address += 33;
3346 addr_incr = MB_STUFFING;
3347 }
3348 } while (addr_incr == MB_STUFFING);
3349 vid_stream->mblock.mb_address += addr_incr;
3350
3351 if (vid_stream->mblock.mb_address > (vid_stream->mb_height *vid_stream->mb_width - 1))
3352 return SKIP_TO_START_CODE;
3353
3354 /*
3355 * If macroblocks have been skipped, process skipped macroblocks.
3356 */
3357
3358 if (vid_stream->mblock.mb_address - vid_stream->mblock.past_mb_addr > 1) {
3359 if (vid_stream->picture.code_type == P_TYPE)
3360 ProcessSkippedPFrameMBlocks(vid_stream);
3361 else if (vid_stream->picture.code_type == B_TYPE)
3362 ProcessSkippedBFrameMBlocks(vid_stream);
3363 }
3364 /* Set past macroblock address to current macroblock address. */
3365 vid_stream->mblock.past_mb_addr = vid_stream->mblock.mb_address;
3366
3367 /* Based on picture type decode macroblock type. */
3368 switch (vid_stream->picture.code_type) {
3369 case I_TYPE:
3370 DecodeMBTypeI(mb_quant, mb_motion_forw, mb_motion_back, mb_pattern,
3371 vid_stream->mblock.mb_intra);
3372 break;
3373
3374 case P_TYPE:
3375 DecodeMBTypeP(mb_quant, mb_motion_forw, mb_motion_back, mb_pattern,
3376 vid_stream->mblock.mb_intra);
3377 break;
3378
3379 case B_TYPE:
3380 DecodeMBTypeB(mb_quant, mb_motion_forw, mb_motion_back, mb_pattern,
3381 vid_stream->mblock.mb_intra);
3382 break;
3383 case D_TYPE:
3384 printf("ERROR: MPEG-1 Streams with D-frames are not supported\n");
3385 exit(1);
3386 }
3387
3388 /* If quantization flag set, parse off new quantization scale. */
3389
3390 if (mb_quant == TRUE) {
3391 get_bits5(data);
3392 vid_stream->slice.quant_scale = data;
3393 }
3394 /* If forward motion vectors exist... */
3395 if (mb_motion_forw == TRUE) {
3396
3397 /* Parse off and decode horizontal forward motion vector. */
3398 DecodeMotionVectors(vid_stream->mblock.motion_h_forw_code);
3399
3400 /* If horiz. forward r data exists, parse off. */
3401
3402 if ((vid_stream->picture.forw_f != 1) &&
3403 (vid_stream->mblock.motion_h_forw_code != 0)) {
3404 get_bitsn(vid_stream->picture.forw_r_size, data);
3405 vid_stream->mblock.motion_h_forw_r = data;
3406 }
3407 /* Parse off and decode vertical forward motion vector. */
3408 DecodeMotionVectors(vid_stream->mblock.motion_v_forw_code);
3409
3410 /* If vert. forw. r data exists, parse off. */
3411
3412 if ((vid_stream->picture.forw_f != 1) &&
3413 (vid_stream->mblock.motion_v_forw_code != 0)) {
3414 get_bitsn(vid_stream->picture.forw_r_size, data);
3415 vid_stream->mblock.motion_v_forw_r = data;
3416 }
3417 }
3418 /* If back motion vectors exist... */
3419 if (mb_motion_back == TRUE) {
3420
3421 /* Parse off and decode horiz. back motion vector. */
3422 DecodeMotionVectors(vid_stream->mblock.motion_h_back_code);
3423
3424 /* If horiz. back r data exists, parse off. */
3425
3426 if ((vid_stream->picture.back_f != 1) &&
3427 (vid_stream->mblock.motion_h_back_code != 0)) {
3428 get_bitsn(vid_stream->picture.back_r_size, data);
3429 vid_stream->mblock.motion_h_back_r = data;
3430 }
3431 /* Parse off and decode vert. back motion vector. */
3432 DecodeMotionVectors(vid_stream->mblock.motion_v_back_code);
3433
3434 /* If vert. back r data exists, parse off. */
3435
3436 if ((vid_stream->picture.back_f != 1) &&
3437 (vid_stream->mblock.motion_v_back_code != 0)) {
3438 get_bitsn(vid_stream->picture.back_r_size, data);
3439 vid_stream->mblock.motion_v_back_r = data;
3440 }
3441 }
3442
3443 /* If mblock pattern flag set, parse and decode CBP (code block pattern). */
3444 if (mb_pattern == TRUE) {
3445 DecodeCBP(vid_stream->mblock.cbp);
3446 } else {
3447 /* Otherwise, set CBP to zero. */
3448 vid_stream->mblock.cbp = 0;
3449 }
3450
3451 /* Reconstruct motion vectors depending on picture type. */
3452 if (vid_stream->picture.code_type == P_TYPE) {
3453
3454 /*
3455 * If no forw motion vectors, reset previous and current vectors to 0.
3456 */
3457
3458 if (!mb_motion_forw) {
3459 recon_right_for = 0;
3460 recon_down_for = 0;
3461 vid_stream->mblock.recon_right_for_prev = 0;
3462 vid_stream->mblock.recon_down_for_prev = 0;
3463 } else {
3464 /*
3465 * Otherwise, compute new forw motion vectors. Reset previous vectors to
3466 * current vectors.
3467 */
3468 ComputeForwVector(&recon_right_for, &recon_down_for, vid_stream);
3469 }
3470 }
3471 if (vid_stream->picture.code_type == B_TYPE) {
3472
3473 /* Reset prev. and current vectors to zero if mblock is intracoded. */
3474
3475 if (vid_stream->mblock.mb_intra) {
3476 vid_stream->mblock.recon_right_for_prev = 0;
3477 vid_stream->mblock.recon_down_for_prev = 0;
3478 vid_stream->mblock.recon_right_back_prev = 0;
3479 vid_stream->mblock.recon_down_back_prev = 0;
3480 } else {
3481
3482 /* If no forw vectors, current vectors equal prev. vectors. */
3483
3484 if (!mb_motion_forw) {
3485 recon_right_for = vid_stream->mblock.recon_right_for_prev;
3486 recon_down_for = vid_stream->mblock.recon_down_for_prev;
3487 } else {
3488 /*
3489 * Otherwise compute forw. vectors. Reset prev vectors to new values.
3490 */
3491 ComputeForwVector(&recon_right_for, &recon_down_for, vid_stream);
3492 }
3493
3494 /* If no back vectors, set back vectors to prev back vectors. */
3495
3496 if (!mb_motion_back) {
3497 recon_right_back = vid_stream->mblock.recon_right_back_prev;
3498 recon_down_back = vid_stream->mblock.recon_down_back_prev;
3499 } else {
3500 /* Otherwise compute new vectors and reset prev. back vectors. */
3501 ComputeBackVector(&recon_right_back, &recon_down_back, vid_stream);
3502 }
3503 /*
3504 * Store vector existence flags in structure for possible skipped
3505 * macroblocks to follow.
3506 */
3507
3508 vid_stream->mblock.bpict_past_forw = mb_motion_forw;
3509 vid_stream->mblock.bpict_past_back = mb_motion_back;
3510 }
3511 }
3512
3513 for (mask = 32, i = 0; i < 6; mask >>= 1, i++) {
3514
3515 /* If block exists... */
3516 if ((vid_stream->mblock.mb_intra) || (vid_stream->mblock.cbp & mask)) {
3517 zero_block_flag = 0;
3518 ParseReconBlock(i, vid_stream);
3519 } else {
3520 zero_block_flag = 1;
3521 }
3522
3523 /* If macroblock is intra coded... */
3524 if (vid_stream->mblock.mb_intra) {
3525 ReconIMBlock(vid_stream, i);
3526 } else if (mb_motion_forw && mb_motion_back) {
3527 ReconBiMBlock(vid_stream, i, recon_right_for, recon_down_for,
3528 recon_right_back, recon_down_back, zero_block_flag);
3529 } else if (mb_motion_forw || (vid_stream->picture.code_type == P_TYPE)) {
3530 ReconPMBlock(vid_stream, i, recon_right_for, recon_down_for,
3531 zero_block_flag);
3532 } else if (mb_motion_back) {
3533 ReconBMBlock(vid_stream, i, recon_right_back, recon_down_back,
3534 zero_block_flag);
3535 }
3536 }
3537
3538 /* If D Type picture, flush marker bit. */
3539 if (vid_stream->picture.code_type == 4)
3540 flush_bits(1);
3541
3542 /* If macroblock was intracoded, set macroblock past intra address. */
3543 if (vid_stream->mblock.mb_intra)
3544 vid_stream->mblock.past_intra_addr =vid_stream->mblock.mb_address;
3545
3546 return PARSE_OK;
3547}
3548
3549
3550
3551/*
3552 *--------------------------------------------------------------
3553 *
3554 * ReconIMBlock --
3555 *
3556 * Reconstructs intra coded macroblock.
3557 *
3558 * Results:
3559 * None.
3560 *
3561 * Side effects:
3562 * None.
3563 *
3564 *--------------------------------------------------------------
3565 */
3566#ifndef NDEBUG
3567/* If people really want to see such things, check 'em */
3568#define myassert(x,expression)\
3569 if (!(expression)) {\
3570 fprintf (stderr,"Bad crop value (%d) at line %d\n", x, __LINE__);\
3571 next_start_code(vid_stream); return;}
3572#define assertCrop(x) myassert(x,((x) >= -MAX_NEG_CROP) && \
3573 ((x) <= 2048+MAX_NEG_CROP))
3574#else
3575#define assertCrop(x)
3576#endif
3577
3578static void ReconIMBlock(mpeg_VidStream *vid_stream, int bnum)
3579{
3580 int mb_row, mb_col, row, col, row_size, rr;
3581 unsigned char *dest;
3582
3583 /* Calculate macroblock row and column from address. */
3584
3585 mb_row = vid_stream->mblock.mb_address / vid_stream->mb_width;
3586 mb_col = vid_stream->mblock.mb_address % vid_stream->mb_width;
3587
3588
3589 /* If block is luminance block... */
3590
3591 if (bnum < 4) {
3592
3593 /* Calculate row and col values for upper left pixel of block. */
3594
3595 row = mb_row * 16;
3596 col = mb_col * 16;
3597 if (bnum > 1)
3598 row += 8;
3599 if (bnum % 2)
3600 col += 8;
3601
3602 /* Set dest to luminance plane of current pict image. */
3603
3604 dest = vid_stream->current->luminance;
3605
3606 /* Establish row size. */
3607
3608 row_size = vid_stream->mb_width * 16;
3609 } else if (bnum == 5) {
3610 /* Otherwise if block is Cr block... */
3611 /* Cr first because of the earlier mixup */
3612
3613 /* Set dest to Cr plane of current pict image. */
3614
3615 dest = vid_stream->current->Cr;
3616
3617 /* Establish row size. */
3618
3619 row_size = vid_stream->mb_width * 8;
3620
3621 /* Calculate row,col for upper left pixel of block. */
3622
3623 row = mb_row * 8;
3624 col = mb_col * 8;
3625 } else {
3626 /* Otherwise block is Cb block, and ... */
3627
3628
3629 /* Set dest to Cb plane of current pict image. */
3630
3631 dest = vid_stream->current->Cb;
3632
3633 /* Establish row size. */
3634
3635 row_size = vid_stream->mb_width * 8;
3636
3637 /* Calculate row,col for upper left pixel value of block. */
3638
3639 row = mb_row * 8;
3640 col = mb_col * 8;
3641 }
3642
3643 /*
3644 * For each pixel in block, set to cropped reconstructed value from inverse
3645 * dct.
3646 */
3647 {
3648 short *sp = &vid_stream->block.dct_recon[0][0];
3649 unsigned char *cm = cropTbl + MAX_NEG_CROP;
3650 dest += row * row_size + col;
3651 for (rr = 0; rr < 4; rr++, sp += 16, dest += row_size) {
3652 dest[0] = cm[sp[0]];
3653 assertCrop(sp[0]);
3654 dest[1] = cm[sp[1]];
3655 assertCrop(sp[1]);
3656 dest[2] = cm[sp[2]];
3657 assertCrop(sp[2]);
3658 dest[3] = cm[sp[3]];
3659 assertCrop(sp[3]);
3660 dest[4] = cm[sp[4]];
3661 assertCrop(sp[4]);
3662 dest[5] = cm[sp[5]];
3663 assertCrop(sp[5]);
3664 dest[6] = cm[sp[6]];
3665 assertCrop(sp[6]);
3666 dest[7] = cm[sp[7]];
3667 assertCrop(sp[7]);
3668
3669 dest += row_size;
3670 dest[0] = cm[sp[8]];
3671 assertCrop(sp[8]);
3672 dest[1] = cm[sp[9]];
3673 assertCrop(sp[9]);
3674 dest[2] = cm[sp[10]];
3675 assertCrop(sp[10]);
3676 dest[3] = cm[sp[11]];
3677 assertCrop(sp[11]);
3678 dest[4] = cm[sp[12]];
3679 assertCrop(sp[12]);
3680 dest[5] = cm[sp[13]];
3681 assertCrop(sp[13]);
3682 dest[6] = cm[sp[14]];
3683 assertCrop(sp[14]);
3684 dest[7] = cm[sp[15]];
3685 assertCrop(sp[15]);
3686 }
3687 }
3688}
3689
3690
3691
3692/*
3693 *--------------------------------------------------------------
3694 *
3695 * ReconPMBlock --
3696 *
3697 * Reconstructs forward predicted macroblocks.
3698 *
3699 * Results:
3700 * None.
3701 *
3702 * Side effects:
3703 * None.
3704 *
3705 *--------------------------------------------------------------
3706 */
3707
3708static void ReconPMBlock(mpeg_VidStream *vid_stream, int bnum, int recon_right_for, int recon_down_for, int zflag)
3709{
3710 int mb_row, mb_col, row, col, row_size, rr;
3711 unsigned char *dest = NULL, *past = NULL;
3712 unsigned char *rindex1, *rindex2;
3713 unsigned char *index;
3714 short int *blockvals;
3715
3716 /* Calculate macroblock row and column from address. */
3717
3718 mb_row = vid_stream->mblock.mb_address / vid_stream->mb_width;
3719 mb_col = vid_stream->mblock.mb_address % vid_stream->mb_width;
3720
3721 if (bnum < 4) {
3722
3723 /* Calculate right_for, down_for motion vectors. */
3724
3725 vid_stream->right_for = recon_right_for >> 1;
3726 vid_stream->down_for = recon_down_for >> 1;
3727 vid_stream->right_half_for = recon_right_for & 0x1;
3728 vid_stream->down_half_for = recon_down_for & 0x1;
3729
3730 /* Set dest to luminance plane of current pict image. */
3731
3732 dest = vid_stream->current->luminance;
3733
3734 if (vid_stream->picture.code_type == B_TYPE) {
3735 if (vid_stream->past != NULL)
3736 past = vid_stream->past->luminance;
3737 } else {
3738
3739 /* Set predictive frame to current future frame. */
3740
3741 if (vid_stream->future != NULL)
3742 past = vid_stream->future->luminance;
3743 }
3744
3745 /* Establish row size. */
3746
3747 row_size = vid_stream->mb_width << 4;
3748
3749 /* Calculate row,col of upper left pixel in block. */
3750
3751 row = mb_row << 4;
3752 col = mb_col << 4;
3753 if (bnum > 1)
3754 row += 8;
3755 if (bnum % 2)
3756 col += 8;
3757 } else {
3758 /* Otherwise, block is NOT luminance block, ... */
3759
3760 /* Construct motion vectors. */
3761
3762 recon_right_for /= 2;
3763 recon_down_for /= 2;
3764 vid_stream->right_for = recon_right_for >> 1;
3765 vid_stream->down_for = recon_down_for >> 1;
3766 vid_stream->right_half_for = recon_right_for & 0x1;
3767 vid_stream->down_half_for = recon_down_for & 0x1;
3768
3769 /* Establish row size. */
3770
3771 row_size = vid_stream->mb_width << 3;
3772
3773 /* Calculate row,col of upper left pixel in block. */
3774
3775 row = mb_row << 3;
3776 col = mb_col << 3;
3777
3778 /* If block is Cr block... */
3779 /* 5 first because order was mixed up in earlier versions */
3780
3781 if (bnum == 5) {
3782
3783 /* Set dest to Cr plane of current pict image. */
3784
3785 dest = vid_stream->current->Cr;
3786
3787 if (vid_stream->picture.code_type == B_TYPE) {
3788
3789 if (vid_stream->past != NULL)
3790 past = vid_stream->past->Cr;
3791 } else {
3792 if (vid_stream->future != NULL)
3793 past = vid_stream->future->Cr;
3794 }
3795 } else {
3796 /* Otherwise, block is Cb block... */
3797 /* Set dest to Cb plane of current pict image. */
3798
3799 dest = vid_stream->current->Cb;
3800
3801 if (vid_stream->picture.code_type == B_TYPE) {
3802 if (vid_stream->past != NULL)
3803 past = vid_stream->past->Cb;
3804 } else {
3805 if (vid_stream->future != NULL)
3806 past = vid_stream->future->Cb;
3807 }
3808 }
3809 }
3810
3811 /* For each pixel in block... */
3812 index = dest + (row * row_size) + col;
3813 rindex1 = past + (row + vid_stream->down_for) * row_size
3814 + col + vid_stream->right_for;
3815
3816 blockvals = &(vid_stream->block.dct_recon[0][0]);
3817
3818 /*
3819 * Calculate predictive pixel value based on motion vectors and copy to
3820 * dest plane.
3821 */
3822
3823 if ((!vid_stream->down_half_for) && (!vid_stream->right_half_for)) {
3824 unsigned char *cm = cropTbl + MAX_NEG_CROP;
3825 if (!zflag) {
3826 for (rr = 0; rr < 4; rr++) {
3827 index[0] = cm[(int) rindex1[0] + (int) blockvals[0]];
3828 index[1] = cm[(int) rindex1[1] + (int) blockvals[1]];
3829 index[2] = cm[(int) rindex1[2] + (int) blockvals[2]];
3830 index[3] = cm[(int) rindex1[3] + (int) blockvals[3]];
3831 index[4] = cm[(int) rindex1[4] + (int) blockvals[4]];
3832 index[5] = cm[(int) rindex1[5] + (int) blockvals[5]];
3833 index[6] = cm[(int) rindex1[6] + (int) blockvals[6]];
3834 index[7] = cm[(int) rindex1[7] + (int) blockvals[7]];
3835 index += row_size;
3836 rindex1 += row_size;
3837
3838 index[0] = cm[(int) rindex1[0] + (int) blockvals[8]];
3839 index[1] = cm[(int) rindex1[1] + (int) blockvals[9]];
3840 index[2] = cm[(int) rindex1[2] + (int) blockvals[10]];
3841 index[3] = cm[(int) rindex1[3] + (int) blockvals[11]];
3842 index[4] = cm[(int) rindex1[4] + (int) blockvals[12]];
3843 index[5] = cm[(int) rindex1[5] + (int) blockvals[13]];
3844 index[6] = cm[(int) rindex1[6] + (int) blockvals[14]];
3845 index[7] = cm[(int) rindex1[7] + (int) blockvals[15]];
3846 blockvals += 16;
3847 index += row_size;
3848 rindex1 += row_size;
3849 }
3850 } else {
3851 if (vid_stream->right_for & 0x1) {
3852 /* No alignment, use bye copy */
3853 for (rr = 0; rr < 4; rr++) {
3854 index[0] = rindex1[0];
3855 index[1] = rindex1[1];
3856 index[2] = rindex1[2];
3857 index[3] = rindex1[3];
3858 index[4] = rindex1[4];
3859 index[5] = rindex1[5];
3860 index[6] = rindex1[6];
3861 index[7] = rindex1[7];
3862 index += row_size;
3863 rindex1 += row_size;
3864
3865 index[0] = rindex1[0];
3866 index[1] = rindex1[1];
3867 index[2] = rindex1[2];
3868 index[3] = rindex1[3];
3869 index[4] = rindex1[4];
3870 index[5] = rindex1[5];
3871 index[6] = rindex1[6];
3872 index[7] = rindex1[7];
3873 index += row_size;
3874 rindex1 += row_size;
3875 }
3876 } else if (vid_stream->right_for & 0x2) {
3877 /* Half-word bit aligned, use 16 bit copy */
3878 short *src = (short *)rindex1;
3879 short *dest = (short *)index;
3880 row_size >>= 1;
3881 for (rr = 0; rr < 4; rr++) {
3882 dest[0] = src[0];
3883 dest[1] = src[1];
3884 dest[2] = src[2];
3885 dest[3] = src[3];
3886 dest += row_size;
3887 src += row_size;
3888
3889 dest[0] = src[0];
3890 dest[1] = src[1];
3891 dest[2] = src[2];
3892 dest[3] = src[3];
3893 dest += row_size;
3894 src += row_size;
3895 }
3896 } else {
3897 /* Word aligned, use 32 bit copy */
3898 int *src = (int *)rindex1;
3899 int *dest = (int *)index;
3900 row_size >>= 2;
3901 for (rr = 0; rr < 4; rr++) {
3902 dest[0] = src[0];
3903 dest[1] = src[1];
3904 dest += row_size;
3905 src += row_size;
3906
3907 dest[0] = src[0];
3908 dest[1] = src[1];
3909 dest += row_size;
3910 src += row_size;
3911 }
3912 }
3913 }
3914 } else {
3915 unsigned char *cm = cropTbl + MAX_NEG_CROP;
3916 rindex2 = rindex1 + vid_stream->right_half_for
3917 + (vid_stream->down_half_for * row_size);
3918
3919 /* if one of the two is zero, then quality makes no difference */
3920
3921 if (!zflag) {
3922 for (rr = 0; rr < 4; rr++) {
3923 index[0] = cm[((int) (rindex1[0] + rindex2[0] + 1) >> 1) + blockvals[0]];
3924 index[1] = cm[((int) (rindex1[1] + rindex2[1] + 1) >> 1) + blockvals[1]];
3925 index[2] = cm[((int) (rindex1[2] + rindex2[2] + 1) >> 1) + blockvals[2]];
3926 index[3] = cm[((int) (rindex1[3] + rindex2[3] + 1) >> 1) + blockvals[3]];
3927 index[4] = cm[((int) (rindex1[4] + rindex2[4] + 1) >> 1) + blockvals[4]];
3928 index[5] = cm[((int) (rindex1[5] + rindex2[5] + 1) >> 1) + blockvals[5]];
3929 index[6] = cm[((int) (rindex1[6] + rindex2[6] + 1) >> 1) + blockvals[6]];
3930 index[7] = cm[((int) (rindex1[7] + rindex2[7] + 1) >> 1) + blockvals[7]];
3931 index += row_size;
3932 rindex1 += row_size;
3933 rindex2 += row_size;
3934
3935 index[0] = cm[((int) (rindex1[0] + rindex2[0] + 1) >> 1) + blockvals[8]];
3936 index[1] = cm[((int) (rindex1[1] + rindex2[1] + 1) >> 1) + blockvals[9]];
3937 index[2] = cm[((int) (rindex1[2] + rindex2[2] + 1) >> 1) + blockvals[10]];
3938 index[3] = cm[((int) (rindex1[3] + rindex2[3] + 1) >> 1) + blockvals[11]];
3939 index[4] = cm[((int) (rindex1[4] + rindex2[4] + 1) >> 1) + blockvals[12]];
3940 index[5] = cm[((int) (rindex1[5] + rindex2[5] + 1) >> 1) + blockvals[13]];
3941 index[6] = cm[((int) (rindex1[6] + rindex2[6] + 1) >> 1) + blockvals[14]];
3942 index[7] = cm[((int) (rindex1[7] + rindex2[7] + 1) >> 1) + blockvals[15]];
3943 blockvals += 16;
3944 index += row_size;
3945 rindex1 += row_size;
3946 rindex2 += row_size;
3947 }
3948 } else { /* zflag */
3949 for (rr = 0; rr < 8; rr++) {
3950 index[0] = (int) (rindex1[0] + rindex2[0] + 1) >> 1;
3951 index[1] = (int) (rindex1[1] + rindex2[1] + 1) >> 1;
3952 index[2] = (int) (rindex1[2] + rindex2[2] + 1) >> 1;
3953 index[3] = (int) (rindex1[3] + rindex2[3] + 1) >> 1;
3954 index[4] = (int) (rindex1[4] + rindex2[4] + 1) >> 1;
3955 index[5] = (int) (rindex1[5] + rindex2[5] + 1) >> 1;
3956 index[6] = (int) (rindex1[6] + rindex2[6] + 1) >> 1;
3957 index[7] = (int) (rindex1[7] + rindex2[7] + 1) >> 1;
3958 index += row_size;
3959 rindex1 += row_size;
3960 rindex2 += row_size;
3961 }
3962 } /* zflag */
3963
3964 }
3965}
3966
3967
3968/*
3969 *--------------------------------------------------------------
3970 *
3971 * ReconBMBlock --
3972 *
3973 * Reconstructs back predicted macroblocks.
3974 *
3975 * Results:
3976 * None.
3977 *
3978 * Side effects:
3979 * None.
3980 *
3981 *--------------------------------------------------------------
3982 */
3983
3984static void
3985ReconBMBlock(vid_stream, bnum, recon_right_back, recon_down_back, zflag)
3987 int bnum, recon_right_back, recon_down_back, zflag;
3988{
3989 int mb_row, mb_col, row, col, row_size, rr;
3990 unsigned char *dest, *future;
3991 int right_back, down_back, right_half_back, down_half_back;
3992 unsigned char *rindex1, *rindex2;
3993 unsigned char *index;
3994 short int *blockvals;
3995
3996 future = NULL;
3997 /* Calculate macroblock row and column from address. */
3998
3999 mb_row = vid_stream->mblock.mb_address / vid_stream->mb_width;
4000 mb_col = vid_stream->mblock.mb_address % vid_stream->mb_width;
4001
4002 /* If block is luminance block... */
4003
4004 if (bnum < 4) {
4005
4006 /* Calculate right_back, down_back motion vectors. */
4007
4008 right_back = recon_right_back >> 1;
4009 down_back = recon_down_back >> 1;
4010 right_half_back = recon_right_back & 0x1;
4011 down_half_back = recon_down_back & 0x1;
4012
4013 /* Set dest to luminance plane of current pict image. */
4014
4015 dest = vid_stream->current->luminance;
4016
4017 /*
4018 * If future frame exists, set future to luminance plane of future frame.
4019 */
4020
4021 if (vid_stream->future != NULL)
4022 future = vid_stream->future->luminance;
4023
4024 /* Establish row size. */
4025
4026 row_size = vid_stream->mb_width << 4;
4027
4028 /* Calculate row,col of upper left pixel in block. */
4029
4030 row = mb_row << 4;
4031 col = mb_col << 4;
4032 if (bnum > 1)
4033 row += 8;
4034 if (bnum % 2)
4035 col += 8;
4036
4037 }
4038 /* Otherwise, block is NOT luminance block, ... */
4039
4040 else {
4041
4042 /* Construct motion vectors. */
4043
4044 recon_right_back /= 2;
4045 recon_down_back /= 2;
4046 right_back = recon_right_back >> 1;
4047 down_back = recon_down_back >> 1;
4048 right_half_back = recon_right_back & 0x1;
4049 down_half_back = recon_down_back & 0x1;
4050
4051 /* Establish row size. */
4052
4053 row_size = vid_stream->mb_width << 3;
4054
4055 /* Calculate row,col of upper left pixel in block. */
4056
4057 row = mb_row << 3;
4058 col = mb_col << 3;
4059
4060 /* If block is Cr block... */
4061 /* They were switched earlier, so 5 is first - eyhung */
4062
4063 if (bnum == 5) {
4064
4065 /* Set dest to Cr plane of current pict image. */
4066
4067 dest = vid_stream->current->Cr;
4068
4069 /*
4070 * If future frame exists, set future to Cr plane of future image.
4071 */
4072
4073 if (vid_stream->future != NULL)
4074 future = vid_stream->future->Cr;
4075 }
4076 /* Otherwise, block is Cb block... */
4077
4078 else {
4079
4080 /* Set dest to Cb plane of current pict image. */
4081
4082 dest = vid_stream->current->Cb;
4083
4084 /*
4085 * If future frame exists, set future to Cb plane of future frame.
4086 */
4087
4088 if (vid_stream->future != NULL)
4089 future = vid_stream->future->Cb;
4090 }
4091 }
4092
4093 /* For each pixel in block do... */
4094
4095 index = dest + (row * row_size) + col;
4096 rindex1 = future + (row + down_back) * row_size + col + right_back;
4097
4098 blockvals = &(vid_stream->block.dct_recon[0][0]);
4099
4100 if ((!right_half_back) && (!down_half_back)) {
4101 unsigned char *cm = cropTbl + MAX_NEG_CROP;
4102 if (!zflag)
4103 for (rr = 0; rr < 4; rr++) {
4104 index[0] = cm[(int) rindex1[0] + (int) blockvals[0]];
4105 index[1] = cm[(int) rindex1[1] + (int) blockvals[1]];
4106 index[2] = cm[(int) rindex1[2] + (int) blockvals[2]];
4107 index[3] = cm[(int) rindex1[3] + (int) blockvals[3]];
4108 index[4] = cm[(int) rindex1[4] + (int) blockvals[4]];
4109 index[5] = cm[(int) rindex1[5] + (int) blockvals[5]];
4110 index[6] = cm[(int) rindex1[6] + (int) blockvals[6]];
4111 index[7] = cm[(int) rindex1[7] + (int) blockvals[7]];
4112 index += row_size;
4113 rindex1 += row_size;
4114
4115 index[0] = cm[(int) rindex1[0] + (int) blockvals[8]];
4116 index[1] = cm[(int) rindex1[1] + (int) blockvals[9]];
4117 index[2] = cm[(int) rindex1[2] + (int) blockvals[10]];
4118 index[3] = cm[(int) rindex1[3] + (int) blockvals[11]];
4119 index[4] = cm[(int) rindex1[4] + (int) blockvals[12]];
4120 index[5] = cm[(int) rindex1[5] + (int) blockvals[13]];
4121 index[6] = cm[(int) rindex1[6] + (int) blockvals[14]];
4122 index[7] = cm[(int) rindex1[7] + (int) blockvals[15]];
4123 blockvals += 16;
4124 index += row_size;
4125 rindex1 += row_size;
4126 }
4127 else {
4128 if (right_back & 0x1) {
4129 /* No alignment, use bye copy */
4130 for (rr = 0; rr < 4; rr++) {
4131 index[0] = rindex1[0];
4132 index[1] = rindex1[1];
4133 index[2] = rindex1[2];
4134 index[3] = rindex1[3];
4135 index[4] = rindex1[4];
4136 index[5] = rindex1[5];
4137 index[6] = rindex1[6];
4138 index[7] = rindex1[7];
4139 index += row_size;
4140 rindex1 += row_size;
4141
4142 index[0] = rindex1[0];
4143 index[1] = rindex1[1];
4144 index[2] = rindex1[2];
4145 index[3] = rindex1[3];
4146 index[4] = rindex1[4];
4147 index[5] = rindex1[5];
4148 index[6] = rindex1[6];
4149 index[7] = rindex1[7];
4150 index += row_size;
4151 rindex1 += row_size;
4152 }
4153 } else if (right_back & 0x2) {
4154 /* Half-word bit aligned, use 16 bit copy */
4155 short *src = (short *)rindex1;
4156 short *dest = (short *)index;
4157 row_size >>= 1;
4158 for (rr = 0; rr < 4; rr++) {
4159 dest[0] = src[0];
4160 dest[1] = src[1];
4161 dest[2] = src[2];
4162 dest[3] = src[3];
4163 dest += row_size;
4164 src += row_size;
4165
4166 dest[0] = src[0];
4167 dest[1] = src[1];
4168 dest[2] = src[2];
4169 dest[3] = src[3];
4170 dest += row_size;
4171 src += row_size;
4172 }
4173 } else {
4174 /* Word aligned, use 32 bit copy */
4175 int *src = (int *)rindex1;
4176 int *dest = (int *)index;
4177 row_size >>= 2;
4178 for (rr = 0; rr < 4; rr++) {
4179 dest[0] = src[0];
4180 dest[1] = src[1];
4181 dest += row_size;
4182 src += row_size;
4183
4184 dest[0] = src[0];
4185 dest[1] = src[1];
4186 dest += row_size;
4187 src += row_size;
4188 }
4189 }
4190 }
4191 } else {
4192 unsigned char *cm = cropTbl + MAX_NEG_CROP;
4193 rindex2 = rindex1 + right_half_back + (down_half_back * row_size);
4194 if (!zflag) {
4195 for (rr = 0; rr < 4; rr++) {
4196 index[0] = cm[((int) (rindex1[0] + rindex2[0] + 1) >> 1) + blockvals[0]];
4197 index[1] = cm[((int) (rindex1[1] + rindex2[1] + 1) >> 1) + blockvals[1]];
4198 index[2] = cm[((int) (rindex1[2] + rindex2[2] + 1) >> 1) + blockvals[2]];
4199 index[3] = cm[((int) (rindex1[3] + rindex2[3] + 1) >> 1) + blockvals[3]];
4200 index[4] = cm[((int) (rindex1[4] + rindex2[4] + 1) >> 1) + blockvals[4]];
4201 index[5] = cm[((int) (rindex1[5] + rindex2[5] + 1) >> 1) + blockvals[5]];
4202 index[6] = cm[((int) (rindex1[6] + rindex2[6] + 1) >> 1) + blockvals[6]];
4203 index[7] = cm[((int) (rindex1[7] + rindex2[7] + 1) >> 1) + blockvals[7]];
4204 index += row_size;
4205 rindex1 += row_size;
4206 rindex2 += row_size;
4207
4208 index[0] = cm[((int) (rindex1[0] + rindex2[0] + 1) >> 1) + blockvals[8]];
4209 index[1] = cm[((int) (rindex1[1] + rindex2[1] + 1) >> 1) + blockvals[9]];
4210 index[2] = cm[((int) (rindex1[2] + rindex2[2] + 1) >> 1) + blockvals[10]];
4211 index[3] = cm[((int) (rindex1[3] + rindex2[3] + 1) >> 1) + blockvals[11]];
4212 index[4] = cm[((int) (rindex1[4] + rindex2[4] + 1) >> 1) + blockvals[12]];
4213 index[5] = cm[((int) (rindex1[5] + rindex2[5] + 1) >> 1) + blockvals[13]];
4214 index[6] = cm[((int) (rindex1[6] + rindex2[6] + 1) >> 1) + blockvals[14]];
4215 index[7] = cm[((int) (rindex1[7] + rindex2[7] + 1) >> 1) + blockvals[15]];
4216 blockvals += 16;
4217 index += row_size;
4218 rindex1 += row_size;
4219 rindex2 += row_size;
4220 }
4221 } else { /* zflag */
4222 for (rr = 0; rr < 8; rr++) {
4223 index[0] = (int) (rindex1[0] + rindex2[0] + 1) >> 1;
4224 index[1] = (int) (rindex1[1] + rindex2[1] + 1) >> 1;
4225 index[2] = (int) (rindex1[2] + rindex2[2] + 1) >> 1;
4226 index[3] = (int) (rindex1[3] + rindex2[3] + 1) >> 1;
4227 index[4] = (int) (rindex1[4] + rindex2[4] + 1) >> 1;
4228 index[5] = (int) (rindex1[5] + rindex2[5] + 1) >> 1;
4229 index[6] = (int) (rindex1[6] + rindex2[6] + 1) >> 1;
4230 index[7] = (int) (rindex1[7] + rindex2[7] + 1) >> 1;
4231 index += row_size;
4232 rindex1 += row_size;
4233 rindex2 += row_size;
4234 }
4235 }
4236 }
4237}
4238
4239
4240/*
4241 *--------------------------------------------------------------
4242 *
4243 * ReconBiMBlock --
4244 *
4245 * Reconstructs bidirectionally predicted macroblocks.
4246 *
4247 * Results:
4248 * None.
4249 *
4250 * Side effects:
4251 * None.
4252 *
4253 *--------------------------------------------------------------
4254 */
4255
4256static void
4257ReconBiMBlock(vid_stream, bnum, recon_right_for, recon_down_for,
4258 recon_right_back, recon_down_back, zflag)
4260 int bnum, recon_right_for, recon_down_for, recon_right_back, recon_down_back;
4261 int zflag;
4262{
4263 int mb_row, mb_col, row, col, row_size, rr;
4264 unsigned char *dest, *past=NULL, *future=NULL;
4265 int right_for, down_for, right_half_for, down_half_for;
4266 int right_back, down_back, right_half_back, down_half_back;
4267 unsigned char *index, *rindex1, *bindex1;
4268 short int *blockvals;
4269 int forw_row_start, back_row_start, forw_col_start, back_col_start;
4270
4271 /* Calculate macroblock row and column from address. */
4272
4273 mb_row = vid_stream->mblock.mb_address / vid_stream->mb_width;
4274 mb_col = vid_stream->mblock.mb_address % vid_stream->mb_width;
4275
4276 /* If block is luminance block... */
4277
4278 if (bnum < 4) {
4279
4280 /*
4281 * Calculate right_for, down_for, right_half_for, down_half_for,
4282 * right_back, down_bakc, right_half_back, and down_half_back, motion
4283 * vectors.
4284 */
4285
4286 right_for = recon_right_for >> 1;
4287 down_for = recon_down_for >> 1;
4288 right_half_for = recon_right_for & 0x1;
4289 down_half_for = recon_down_for & 0x1;
4290
4291 right_back = recon_right_back >> 1;
4292 down_back = recon_down_back >> 1;
4293 right_half_back = recon_right_back & 0x1;
4294 down_half_back = recon_down_back & 0x1;
4295
4296 /* Set dest to luminance plane of current pict image. */
4297
4298 dest = vid_stream->current->luminance;
4299
4300 /* If past frame exists, set past to luminance plane of past frame. */
4301
4302 if (vid_stream->past != NULL)
4303 past = vid_stream->past->luminance;
4304
4305 /*
4306 * If future frame exists, set future to luminance plane of future frame.
4307 */
4308
4309 if (vid_stream->future != NULL)
4310 future = vid_stream->future->luminance;
4311
4312 /* Establish row size. */
4313
4314 row_size = (vid_stream->mb_width << 4);
4315
4316 /* Calculate row,col of upper left pixel in block. */
4317
4318 row = (mb_row << 4);
4319 col = (mb_col << 4);
4320 if (bnum > 1)
4321 row += 8;
4322 if (bnum & 0x01)
4323 col += 8;
4324
4325 forw_col_start = col + right_for;
4326 forw_row_start = row + down_for;
4327
4328 back_col_start = col + right_back;
4329 back_row_start = row + down_back;
4330
4331 }
4332 /* Otherwise, block is NOT luminance block, ... */
4333
4334 else {
4335
4336 /* Construct motion vectors. */
4337
4338 recon_right_for /= 2;
4339 recon_down_for /= 2;
4340 right_for = recon_right_for >> 1;
4341 down_for = recon_down_for >> 1;
4342 right_half_for = recon_right_for & 0x1;
4343 down_half_for = recon_down_for & 0x1;
4344
4345 recon_right_back /= 2;
4346 recon_down_back /= 2;
4347 right_back = recon_right_back >> 1;
4348 down_back = recon_down_back >> 1;
4349 right_half_back = recon_right_back & 0x1;
4350 down_half_back = recon_down_back & 0x1;
4351
4352 /* Establish row size. */
4353
4354 row_size = (vid_stream->mb_width << 3);
4355
4356 /* Calculate row,col of upper left pixel in block. */
4357
4358 row = (mb_row << 3);
4359 col = (mb_col << 3);
4360
4361 forw_col_start = col + right_for;
4362 forw_row_start = row + down_for;
4363
4364 back_col_start = col + right_back;
4365 back_row_start = row + down_back;
4366
4367 /* If block is Cr block... */
4368 /* Switched earlier, so we test Cr first - eyhung */
4369
4370 if (bnum == 5) {
4371
4372 /* Set dest to Cr plane of current pict image. */
4373
4374 dest = vid_stream->current->Cr;
4375
4376 /* If past frame exists, set past to Cr plane of past image. */
4377
4378 if (vid_stream->past != NULL)
4379 past = vid_stream->past->Cr;
4380
4381 /*
4382 * If future frame exists, set future to Cr plane of future image.
4383 */
4384
4385 if (vid_stream->future != NULL)
4386 future = vid_stream->future->Cr;
4387 }
4388 /* Otherwise, block is Cb block... */
4389
4390 else {
4391
4392 /* Set dest to Cb plane of current pict image. */
4393
4394 dest = vid_stream->current->Cb;
4395
4396 /* If past frame exists, set past to Cb plane of past frame. */
4397
4398 if (vid_stream->past != NULL)
4399 past = vid_stream->past->Cb;
4400
4401 /*
4402 * If future frame exists, set future to Cb plane of future frame.
4403 */
4404
4405 if (vid_stream->future != NULL)
4406 future = vid_stream->future->Cb;
4407 }
4408 }
4409
4410 /* For each pixel in block... */
4411
4412 index = dest + (row * row_size) + col;
4413
4414 rindex1 = past + forw_row_start * row_size + forw_col_start;
4415
4416 bindex1 = future + back_row_start * row_size + back_col_start;
4417
4418 blockvals = (short int *) &(vid_stream->block.dct_recon[0][0]);
4419
4420 {
4421 unsigned char *cm = cropTbl + MAX_NEG_CROP;
4422 if (!zflag)
4423 for (rr = 0; rr < 4; rr++) {
4424 index[0] = cm[((int) (rindex1[0] + bindex1[0]) >> 1) + blockvals[0]];
4425 index[1] = cm[((int) (rindex1[1] + bindex1[1]) >> 1) + blockvals[1]];
4426 index[2] = cm[((int) (rindex1[2] + bindex1[2]) >> 1) + blockvals[2]];
4427 index[3] = cm[((int) (rindex1[3] + bindex1[3]) >> 1) + blockvals[3]];
4428 index[4] = cm[((int) (rindex1[4] + bindex1[4]) >> 1) + blockvals[4]];
4429 index[5] = cm[((int) (rindex1[5] + bindex1[5]) >> 1) + blockvals[5]];
4430 index[6] = cm[((int) (rindex1[6] + bindex1[6]) >> 1) + blockvals[6]];
4431 index[7] = cm[((int) (rindex1[7] + bindex1[7]) >> 1) + blockvals[7]];
4432 index += row_size;
4433 rindex1 += row_size;
4434 bindex1 += row_size;
4435
4436 index[0] = cm[((int) (rindex1[0] + bindex1[0]) >> 1) + blockvals[8]];
4437 index[1] = cm[((int) (rindex1[1] + bindex1[1]) >> 1) + blockvals[9]];
4438 index[2] = cm[((int) (rindex1[2] + bindex1[2]) >> 1) + blockvals[10]];
4439 index[3] = cm[((int) (rindex1[3] + bindex1[3]) >> 1) + blockvals[11]];
4440 index[4] = cm[((int) (rindex1[4] + bindex1[4]) >> 1) + blockvals[12]];
4441 index[5] = cm[((int) (rindex1[5] + bindex1[5]) >> 1) + blockvals[13]];
4442 index[6] = cm[((int) (rindex1[6] + bindex1[6]) >> 1) + blockvals[14]];
4443 index[7] = cm[((int) (rindex1[7] + bindex1[7]) >> 1) + blockvals[15]];
4444 blockvals += 16;
4445 index += row_size;
4446 rindex1 += row_size;
4447 bindex1 += row_size;
4448 }
4449
4450 else
4451 for (rr = 0; rr < 4; rr++) {
4452 index[0] = (int) (rindex1[0] + bindex1[0]) >> 1;
4453 index[1] = (int) (rindex1[1] + bindex1[1]) >> 1;
4454 index[2] = (int) (rindex1[2] + bindex1[2]) >> 1;
4455 index[3] = (int) (rindex1[3] + bindex1[3]) >> 1;
4456 index[4] = (int) (rindex1[4] + bindex1[4]) >> 1;
4457 index[5] = (int) (rindex1[5] + bindex1[5]) >> 1;
4458 index[6] = (int) (rindex1[6] + bindex1[6]) >> 1;
4459 index[7] = (int) (rindex1[7] + bindex1[7]) >> 1;
4460 index += row_size;
4461 rindex1 += row_size;
4462 bindex1 += row_size;
4463
4464 index[0] = (int) (rindex1[0] + bindex1[0]) >> 1;
4465 index[1] = (int) (rindex1[1] + bindex1[1]) >> 1;
4466 index[2] = (int) (rindex1[2] + bindex1[2]) >> 1;
4467 index[3] = (int) (rindex1[3] + bindex1[3]) >> 1;
4468 index[4] = (int) (rindex1[4] + bindex1[4]) >> 1;
4469 index[5] = (int) (rindex1[5] + bindex1[5]) >> 1;
4470 index[6] = (int) (rindex1[6] + bindex1[6]) >> 1;
4471 index[7] = (int) (rindex1[7] + bindex1[7]) >> 1;
4472 index += row_size;
4473 rindex1 += row_size;
4474 bindex1 += row_size;
4475 }
4476 }
4477}
4478
4479
4480/*
4481 *--------------------------------------------------------------
4482 *
4483 * ProcessSkippedPFrameMBlocks --
4484 *
4485 * Processes skipped macroblocks in P frames.
4486 *
4487 * Results:
4488 * Calculates pixel values for luminance, Cr, and Cb planes
4489 * in current pict image for skipped macroblocks.
4490 *
4491 * Side effects:
4492 * Pixel values in pict image changed.
4493 *
4494 *--------------------------------------------------------------
4495 */
4496
4497static void
4498ProcessSkippedPFrameMBlocks(vid_stream)
4500{
4501 int row_size, half_row, mb_row, mb_col, row, col, rr;
4502 int addr, row_incr, half_row_incr, crow, ccol;
4503 int *dest, *src, *dest1, *src1;
4504
4505 /* Calculate row sizes for luminance and Cr/Cb macroblock areas. */
4506
4507 row_size = vid_stream->mb_width << 4;
4508 half_row = (row_size >> 1);
4509 row_incr = row_size >> 2;
4510 half_row_incr = half_row >> 2;
4511
4512 /* For each skipped macroblock, do... */
4513
4514 for (addr = vid_stream->mblock.past_mb_addr + 1;
4515 addr < vid_stream->mblock.mb_address; addr++) {
4516
4517 /* Calculate macroblock row and col. */
4518
4519 mb_row = addr / vid_stream->mb_width;
4520 mb_col = addr % vid_stream->mb_width;
4521
4522 /* Calculate upper left pixel row,col for luminance plane. */
4523
4524 row = mb_row << 4;
4525 col = mb_col << 4;
4526
4527
4528 /* For each row in macroblock luminance plane... */
4529
4530 dest = (int *)(vid_stream->current->luminance + (row * row_size) + col);
4531 src = (int *)(vid_stream->future->luminance + (row * row_size) + col);
4532
4533 for (rr = 0; rr < 8; rr++) {
4534
4535 /* Copy pixel values from last I or P picture. */
4536
4537 dest[0] = src[0];
4538 dest[1] = src[1];
4539 dest[2] = src[2];
4540 dest[3] = src[3];
4541 dest += row_incr;
4542 src += row_incr;
4543
4544 dest[0] = src[0];
4545 dest[1] = src[1];
4546 dest[2] = src[2];
4547 dest[3] = src[3];
4548 dest += row_incr;
4549 src += row_incr;
4550 }
4551
4552 /*
4553 * Divide row,col to get upper left pixel of macroblock in Cr and Cb
4554 * planes.
4555 */
4556
4557 crow = row >> 1;
4558 ccol = col >> 1;
4559
4560 /* For each row in Cr, and Cb planes... */
4561
4562 dest = (int *)(vid_stream->current->Cr + (crow * half_row) + ccol);
4563 src = (int *)(vid_stream->future->Cr + (crow * half_row) + ccol);
4564 dest1 = (int *)(vid_stream->current->Cb + (crow * half_row) + ccol);
4565 src1 = (int *)(vid_stream->future->Cb + (crow * half_row) + ccol);
4566
4567 for (rr = 0; rr < 4; rr++) {
4568
4569 /* Copy pixel values from last I or P picture. */
4570
4571 dest[0] = src[0];
4572 dest[1] = src[1];
4573
4574 dest1[0] = src1[0];
4575 dest1[1] = src1[1];
4576
4577 dest += half_row_incr;
4578 src += half_row_incr;
4579 dest1 += half_row_incr;
4580 src1 += half_row_incr;
4581
4582 dest[0] = src[0];
4583 dest[1] = src[1];
4584
4585 dest1[0] = src1[0];
4586 dest1[1] = src1[1];
4587
4588 dest += half_row_incr;
4589 src += half_row_incr;
4590 dest1 += half_row_incr;
4591 src1 += half_row_incr;
4592 }
4593
4594 }
4595
4596 vid_stream->mblock.recon_right_for_prev = 0;
4597 vid_stream->mblock.recon_down_for_prev = 0;
4598}
4599
4600
4601
4602
4603
4604/*
4605 *--------------------------------------------------------------
4606 *
4607 * ProcessSkippedBFrameMBlocks --
4608 *
4609 * Processes skipped macroblocks in B frames.
4610 *
4611 * Results:
4612 * Calculates pixel values for luminance, Cr, and Cb planes
4613 * in current pict image for skipped macroblocks.
4614 *
4615 * Side effects:
4616 * Pixel values in pict image changed.
4617 *
4618 *--------------------------------------------------------------
4619 */
4620
4621static void
4622ProcessSkippedBFrameMBlocks(vid_stream)
4624{
4625 int row_size, half_row, mb_row, mb_col, row, col, rr;
4626 int right_half_for = 0, down_half_for = 0;
4627 int c_right_half_for = 0, c_down_half_for = 0;
4628 int right_half_back = 0, down_half_back = 0;
4629 int c_right_half_back = 0, c_down_half_back = 0;
4630 int addr, right_for = 0, down_for = 0;
4631 int recon_right_for, recon_down_for;
4632 int recon_right_back, recon_down_back;
4633 int right_back = 0, down_back = 0;
4634 int c_right_for = 0, c_down_for = 0;
4635 int c_right_back = 0, c_down_back = 0;
4636 unsigned char forw_lum[256];
4637 unsigned char forw_cr[64], forw_cb[64];
4638 unsigned char back_lum[256], back_cr[64], back_cb[64];
4639 int row_incr, half_row_incr;
4640 int ccol, crow;
4641
4642 /* Calculate row sizes for luminance and Cr/Cb macroblock areas. */
4643
4644 row_size = vid_stream->mb_width << 4;
4645 half_row = (row_size >> 1);
4646 row_incr = row_size >> 2;
4647 half_row_incr = half_row >> 2;
4648
4649 /* Establish motion vector codes based on full pixel flag. */
4650
4651 if (vid_stream->picture.full_pel_forw_vector) {
4652 recon_right_for = vid_stream->mblock.recon_right_for_prev << 1;
4653 recon_down_for = vid_stream->mblock.recon_down_for_prev << 1;
4654 } else {
4655 recon_right_for = vid_stream->mblock.recon_right_for_prev;
4656 recon_down_for = vid_stream->mblock.recon_down_for_prev;
4657 }
4658
4659 if (vid_stream->picture.full_pel_back_vector) {
4660 recon_right_back = vid_stream->mblock.recon_right_back_prev << 1;
4661 recon_down_back = vid_stream->mblock.recon_down_back_prev << 1;
4662 } else {
4663 recon_right_back = vid_stream->mblock.recon_right_back_prev;
4664 recon_down_back = vid_stream->mblock.recon_down_back_prev;
4665 }
4666
4667
4668 /* If only one motion vector, do display copy, else do full
4669 calculation.
4670 */
4671
4672 /* Calculate motion vectors. */
4673
4674 if (vid_stream->mblock.bpict_past_forw) {
4675 right_for = recon_right_for >> 1;
4676 down_for = recon_down_for >> 1;
4677 right_half_for = recon_right_for & 0x1;
4678 down_half_for = recon_down_for & 0x1;
4679
4680 recon_right_for /= 2;
4681 recon_down_for /= 2;
4682 c_right_for = recon_right_for >> 1;
4683 c_down_for = recon_down_for >> 1;
4684 c_right_half_for = recon_right_for & 0x1;
4685 c_down_half_for = recon_down_for & 0x1;
4686
4687 }
4688 if (vid_stream->mblock.bpict_past_back) {
4689 right_back = recon_right_back >> 1;
4690 down_back = recon_down_back >> 1;
4691 right_half_back = recon_right_back & 0x1;
4692 down_half_back = recon_down_back & 0x1;
4693
4694 recon_right_back /= 2;
4695 recon_down_back /= 2;
4696 c_right_back = recon_right_back >> 1;
4697 c_down_back = recon_down_back >> 1;
4698 c_right_half_back = recon_right_back & 0x1;
4699 c_down_half_back = recon_down_back & 0x1;
4700
4701 }
4702 /* For each skipped macroblock, do... */
4703
4704 for (addr = vid_stream->mblock.past_mb_addr + 1;
4705 addr < vid_stream->mblock.mb_address; addr++) {
4706
4707 /* Calculate macroblock row and col. */
4708
4709 mb_row = addr / vid_stream->mb_width;
4710 mb_col = addr % vid_stream->mb_width;
4711
4712 /* Calculate upper left pixel row,col for luminance plane. */
4713
4714 row = mb_row << 4;
4715 col = mb_col << 4;
4716 crow = row / 2;
4717 ccol = col / 2;
4718
4719 /* If forward predicted, calculate prediction values. */
4720
4721 if (vid_stream->mblock.bpict_past_forw) {
4722
4723 ReconSkippedBlock(vid_stream->past->luminance, forw_lum,
4724 row, col, row_size, right_for, down_for,
4725 right_half_for, down_half_for, 16);
4726 ReconSkippedBlock(vid_stream->past->Cr, forw_cr, crow,
4727 ccol, half_row,
4728 c_right_for, c_down_for, c_right_half_for, c_down_half_for, 8);
4729 ReconSkippedBlock(vid_stream->past->Cb, forw_cb, crow,
4730 ccol, half_row,
4731 c_right_for, c_down_for, c_right_half_for, c_down_half_for, 8);
4732 }
4733 /* If back predicted, calculate prediction values. */
4734
4735 if (vid_stream->mblock.bpict_past_back) {
4736 ReconSkippedBlock(vid_stream->future->luminance, back_lum,
4737 row, col, row_size, right_back, down_back,
4738 right_half_back, down_half_back, 16);
4739 ReconSkippedBlock(vid_stream->future->Cr, back_cr, crow,
4740 ccol, half_row,
4741 c_right_back, c_down_back,
4742 c_right_half_back, c_down_half_back, 8);
4743 ReconSkippedBlock(vid_stream->future->Cb, back_cb, crow,
4744 ccol, half_row,
4745 c_right_back, c_down_back,
4746 c_right_half_back, c_down_half_back, 8);
4747 }
4748 if (vid_stream->mblock.bpict_past_forw &&
4749 !vid_stream->mblock.bpict_past_back) {
4750
4751 int *dest, *dest1;
4752 int *src, *src1;
4753 dest = (int *)(vid_stream->current->luminance + (row * row_size) + col);
4754 src = (int *)forw_lum;
4755
4756 for (rr = 0; rr < 16; rr++) {
4757
4758 /* memcpy(dest, forw_lum+(rr<<4), 16); */
4759 dest[0] = src[0];
4760 dest[1] = src[1];
4761 dest[2] = src[2];
4762 dest[3] = src[3];
4763 dest += row_incr;
4764 src += 4;
4765 }
4766
4767 dest = (int *)(vid_stream->current->Cr + (crow * half_row) + ccol);
4768 dest1 = (int *)(vid_stream->current->Cb + (crow * half_row) + ccol);
4769 src = (int *)forw_cr;
4770 src1 = (int *)forw_cb;
4771
4772 for (rr = 0; rr < 8; rr++) {
4773 /*
4774 * memcpy(dest, forw_cr+(rr<<3), 8); memcpy(dest1, forw_cb+(rr<<3),
4775 * 8);
4776 */
4777
4778 dest[0] = src[0];
4779 dest[1] = src[1];
4780
4781 dest1[0] = src1[0];
4782 dest1[1] = src1[1];
4783
4784 dest += half_row_incr;
4785 dest1 += half_row_incr;
4786 src += 2;
4787 src1 += 2;
4788 }
4789 } else if (vid_stream->mblock.bpict_past_back &&
4790 !vid_stream->mblock.bpict_past_forw) {
4791
4792 int *src, *src1;
4793 int *dest, *dest1;
4794 dest = (int *)(vid_stream->current->luminance + (row * row_size) + col);
4795 src = (int *)back_lum;
4796
4797 for (rr = 0; rr < 16; rr++) {
4798 dest[0] = src[0];
4799 dest[1] = src[1];
4800 dest[2] = src[2];
4801 dest[3] = src[3];
4802 dest += row_incr;
4803 src += 4;
4804 }
4805
4806
4807 dest = (int *)(vid_stream->current->Cr + (crow * half_row) + ccol);
4808 dest1 = (int *)(vid_stream->current->Cb + (crow * half_row) + ccol);
4809 src = (int *)back_cr;
4810 src1 = (int *)back_cb;
4811
4812 for (rr = 0; rr < 8; rr++) {
4813 /*
4814 * memcpy(dest, back_cr+(rr<<3), 8); memcpy(dest1, back_cb+(rr<<3),
4815 * 8);
4816 */
4817
4818 dest[0] = src[0];
4819 dest[1] = src[1];
4820
4821 dest1[0] = src1[0];
4822 dest1[1] = src1[1];
4823
4824 dest += half_row_incr;
4825 dest1 += half_row_incr;
4826 src += 2;
4827 src1 += 2;
4828 }
4829 } else {
4830
4831 unsigned char *src1, *src2, *src1a, *src2a;
4832 unsigned char *dest, *dest1;
4833 dest = vid_stream->current->luminance + (row * row_size) + col;
4834 src1 = forw_lum;
4835 src2 = back_lum;
4836
4837 for (rr = 0; rr < 16; rr++) {
4838 dest[0] = (int) (src1[0] + src2[0]) >> 1;
4839 dest[1] = (int) (src1[1] + src2[1]) >> 1;
4840 dest[2] = (int) (src1[2] + src2[2]) >> 1;
4841 dest[3] = (int) (src1[3] + src2[3]) >> 1;
4842 dest[4] = (int) (src1[4] + src2[4]) >> 1;
4843 dest[5] = (int) (src1[5] + src2[5]) >> 1;
4844 dest[6] = (int) (src1[6] + src2[6]) >> 1;
4845 dest[7] = (int) (src1[7] + src2[7]) >> 1;
4846 dest[8] = (int) (src1[8] + src2[8]) >> 1;
4847 dest[9] = (int) (src1[9] + src2[9]) >> 1;
4848 dest[10] = (int) (src1[10] + src2[10]) >> 1;
4849 dest[11] = (int) (src1[11] + src2[11]) >> 1;
4850 dest[12] = (int) (src1[12] + src2[12]) >> 1;
4851 dest[13] = (int) (src1[13] + src2[13]) >> 1;
4852 dest[14] = (int) (src1[14] + src2[14]) >> 1;
4853 dest[15] = (int) (src1[15] + src2[15]) >> 1;
4854 dest += row_size;
4855 src1 += 16;
4856 src2 += 16;
4857 }
4858
4859
4860 dest = vid_stream->current->Cr + (crow * half_row) + ccol;
4861 dest1 = vid_stream->current->Cb + (crow * half_row) + ccol;
4862 src1 = forw_cr;
4863 src2 = back_cr;
4864 src1a = forw_cb;
4865 src2a = back_cb;
4866
4867 for (rr = 0; rr < 8; rr++) {
4868 dest[0] = (int) (src1[0] + src2[0]) >> 1;
4869 dest[1] = (int) (src1[1] + src2[1]) >> 1;
4870 dest[2] = (int) (src1[2] + src2[2]) >> 1;
4871 dest[3] = (int) (src1[3] + src2[3]) >> 1;
4872 dest[4] = (int) (src1[4] + src2[4]) >> 1;
4873 dest[5] = (int) (src1[5] + src2[5]) >> 1;
4874 dest[6] = (int) (src1[6] + src2[6]) >> 1;
4875 dest[7] = (int) (src1[7] + src2[7]) >> 1;
4876 dest += half_row;
4877 src1 += 8;
4878 src2 += 8;
4879
4880 dest1[0] = (int) (src1a[0] + src2a[0]) >> 1;
4881 dest1[1] = (int) (src1a[1] + src2a[1]) >> 1;
4882 dest1[2] = (int) (src1a[2] + src2a[2]) >> 1;
4883 dest1[3] = (int) (src1a[3] + src2a[3]) >> 1;
4884 dest1[4] = (int) (src1a[4] + src2a[4]) >> 1;
4885 dest1[5] = (int) (src1a[5] + src2a[5]) >> 1;
4886 dest1[6] = (int) (src1a[6] + src2a[6]) >> 1;
4887 dest1[7] = (int) (src1a[7] + src2a[7]) >> 1;
4888 dest1 += half_row;
4889 src1a += 8;
4890 src2a += 8;
4891 }
4892 }
4893
4894 }
4895}
4896
4897
4898
4899
4900
4901/*
4902 *--------------------------------------------------------------
4903 *
4904 * ReconSkippedBlock --
4905 *
4906 * Reconstructs predictive block for skipped macroblocks
4907 * in B Frames.
4908 *
4909 * Results:
4910 * No return values.
4911 *
4912 * Side effects:
4913 * None.
4914 *
4915 *--------------------------------------------------------------
4916 */
4917
4918static void
4919ReconSkippedBlock(source, dest, row, col, row_size,
4920 right, down, right_half, down_half, width)
4921 unsigned char *source;
4922 unsigned char *dest;
4923 int row, col, row_size, right, down, right_half, down_half, width;
4924{
4925 int rr;
4926 unsigned char *source2;
4927
4928 source += ((row + down) * row_size) + col + right;
4929
4930 if (width == 16) {
4931 if ((!right_half) && (!down_half)) {
4932 if (right & 0x1) {
4933 /* No alignment, use bye copy */
4934 for (rr = 0; rr < 16; rr++) {
4935 dest[0] = source[0];
4936 dest[1] = source[1];
4937 dest[2] = source[2];
4938 dest[3] = source[3];
4939 dest[4] = source[4];
4940 dest[5] = source[5];
4941 dest[6] = source[6];
4942 dest[7] = source[7];
4943 dest[8] = source[8];
4944 dest[9] = source[9];
4945 dest[10] = source[10];
4946 dest[11] = source[11];
4947 dest[12] = source[12];
4948 dest[13] = source[13];
4949 dest[14] = source[14];
4950 dest[15] = source[15];
4951 dest += 16;
4952 source += row_size;
4953 }
4954 } else if (right & 0x2) {
4955 /* Half-word bit aligned, use 16 bit copy */
4956 short *src = (short *)source;
4957 short *d = (short *)dest;
4958 row_size >>= 1;
4959 for (rr = 0; rr < 16; rr++) {
4960 d[0] = src[0];
4961 d[1] = src[1];
4962 d[2] = src[2];
4963 d[3] = src[3];
4964 d[4] = src[4];
4965 d[5] = src[5];
4966 d[6] = src[6];
4967 d[7] = src[7];
4968 d += 8;
4969 src += row_size;
4970 }
4971 } else {
4972 /* Word aligned, use 32 bit copy */
4973 int *src = (int *)source;
4974 int *d = (int *)dest;
4975 row_size >>= 2;
4976 for (rr = 0; rr < 16; rr++) {
4977 d[0] = src[0];
4978 d[1] = src[1];
4979 d[2] = src[2];
4980 d[3] = src[3];
4981 d += 4;
4982 src += row_size;
4983 }
4984 }
4985 } else {
4986 source2 = source + right_half + (row_size * down_half);
4987 for (rr = 0; rr < width; rr++) {
4988 dest[0] = (int) (source[0] + source2[0]) >> 1;
4989 dest[1] = (int) (source[1] + source2[1]) >> 1;
4990 dest[2] = (int) (source[2] + source2[2]) >> 1;
4991 dest[3] = (int) (source[3] + source2[3]) >> 1;
4992 dest[4] = (int) (source[4] + source2[4]) >> 1;
4993 dest[5] = (int) (source[5] + source2[5]) >> 1;
4994 dest[6] = (int) (source[6] + source2[6]) >> 1;
4995 dest[7] = (int) (source[7] + source2[7]) >> 1;
4996 dest[8] = (int) (source[8] + source2[8]) >> 1;
4997 dest[9] = (int) (source[9] + source2[9]) >> 1;
4998 dest[10] = (int) (source[10] + source2[10]) >> 1;
4999 dest[11] = (int) (source[11] + source2[11]) >> 1;
5000 dest[12] = (int) (source[12] + source2[12]) >> 1;
5001 dest[13] = (int) (source[13] + source2[13]) >> 1;
5002 dest[14] = (int) (source[14] + source2[14]) >> 1;
5003 dest[15] = (int) (source[15] + source2[15]) >> 1;
5004 dest += width;
5005 source += row_size;
5006 source2 += row_size;
5007 }
5008 }
5009 } else { /* (width == 8) */
5010 assert(width == 8);
5011 if ((!right_half) && (!down_half)) {
5012 if (right & 0x1) {
5013 for (rr = 0; rr < width; rr++) {
5014 dest[0] = source[0];
5015 dest[1] = source[1];
5016 dest[2] = source[2];
5017 dest[3] = source[3];
5018 dest[4] = source[4];
5019 dest[5] = source[5];
5020 dest[6] = source[6];
5021 dest[7] = source[7];
5022 dest += 8;
5023 source += row_size;
5024 }
5025 } else if (right & 0x02) {
5026 short *d = (short *)dest;
5027 short *src = (short *)source;
5028 row_size >>= 1;
5029 for (rr = 0; rr < width; rr++) {
5030 d[0] = src[0];
5031 d[1] = src[1];
5032 d[2] = src[2];
5033 d[3] = src[3];
5034 d += 4;
5035 src += row_size;
5036 }
5037 } else {
5038 int *d = (int *)dest;
5039 int *src = (int *)source;
5040 row_size >>= 2;
5041 for (rr = 0; rr < width; rr++) {
5042 d[0] = src[0];
5043 d[1] = src[1];
5044 d += 2;
5045 src += row_size;
5046 }
5047 }
5048 } else {
5049 source2 = source + right_half + (row_size * down_half);
5050 for (rr = 0; rr < width; rr++) {
5051 dest[0] = (int) (source[0] + source2[0]) >> 1;
5052 dest[1] = (int) (source[1] + source2[1]) >> 1;
5053 dest[2] = (int) (source[2] + source2[2]) >> 1;
5054 dest[3] = (int) (source[3] + source2[3]) >> 1;
5055 dest[4] = (int) (source[4] + source2[4]) >> 1;
5056 dest[5] = (int) (source[5] + source2[5]) >> 1;
5057 dest[6] = (int) (source[6] + source2[6]) >> 1;
5058 dest[7] = (int) (source[7] + source2[7]) >> 1;
5059 dest += width;
5060 source += row_size;
5061 source2 += row_size;
5062 }
5063 }
5064 }
5065}
5066
5067
5068
5069
5070/*
5071 *--------------------------------------------------------------
5072 *
5073 * DoPictureDisplay --
5074 *
5075 * Converts image from Lum, Cr, Cb to colormap space. Puts
5076 * image in lum plane. Updates past and future frame
5077 * pointers. Dithers image. Sends to display mechanism.
5078 *
5079 * Results:
5080 * Pict image structure locked if displaying or if frame
5081 * is needed as past or future reference.
5082 *
5083 * Side effects:
5084 * Lum plane pummelled.
5085 *
5086 *--------------------------------------------------------------
5087 */
5088
5089static void
5090DoPictureDisplay(vid_stream)
5092{
5093
5094 if (No_B_Flag && (vid_stream->picture.code_type == B_TYPE)) return;
5095
5096 /* Convert to colormap space and dither. */
5097 Color32DitherImage(vid_stream->current->luminance,
5098 vid_stream->current->Cr,
5099 vid_stream->current->Cb,
5100 vid_stream->current->display,
5101 vid_stream->mb_height * 16,
5102 vid_stream->mb_width * 16);
5103
5104
5105
5106 /* Update past and future references if needed. */
5107
5108 if ((vid_stream->picture.code_type == I_TYPE) || (vid_stream->picture.code_type == P_TYPE)) {
5109 if (vid_stream->future == NULL) {
5110 vid_stream->future = vid_stream->current;
5111 vid_stream->future->locked |= FUTURE_LOCK;
5112 } else {
5113 if (vid_stream->past != NULL) {
5114 vid_stream->past->locked &= ~PAST_LOCK;
5115 }
5116 vid_stream->past = vid_stream->future;
5117 vid_stream->past->locked &= ~FUTURE_LOCK;
5118 vid_stream->past->locked |= PAST_LOCK;
5119 vid_stream->future = vid_stream->current;
5120 vid_stream->future->locked |= FUTURE_LOCK;
5121 vid_stream->current = vid_stream->past;
5122 ExecuteTexture(vid_stream);
5123 }
5124 } else {
5125 ExecuteTexture(vid_stream);
5126 }
5127}
5128
5129
5130
5131
5132#define GLOBAL /* a function referenced thru EXTERNs */
5133
5134/* We assume that right shift corresponds to signed division by 2 with
5135 * rounding towards minus infinity. This is correct for typical "arithmetic
5136 * shift" instructions that shift in copies of the sign bit. But some
5137 * C compilers implement >> with an unsigned shift. For these machines you
5138 * must define RIGHT_SHIFT_IS_UNSIGNED.
5139 * RIGHT_SHIFT provides a proper signed right shift of an int quantity.
5140 * It is only applied with constant shift counts. SHIFT_TEMPS must be
5141 * included in the variables of any routine using RIGHT_SHIFT.
5142 */
5143
5144#ifdef RIGHT_SHIFT_IS_UNSIGNED
5145#define SHIFT_TEMPS int shift_temp;
5146#define RIGHT_SHIFT(x,shft) \
5147 ((shift_temp = (x)) < 0 ? \
5148 (shift_temp >> (shft)) | ((~((int) 0)) << (32-(shft))) : \
5149 (shift_temp >> (shft)))
5150#else
5151#define SHIFT_TEMPS
5152#define RIGHT_SHIFT(x,shft) ((x) >> (shft))
5153#endif
5154
5155/*
5156 * This routine is specialized to the case DCTSIZE = 8.
5157 */
5158
5159#if DCTSIZE != 8
5160 Sorry, this code only copes with 8x8 DCTs. /* deliberate syntax err */
5161#endif
5162
5163
5164/*
5165 * A 2-D IDCT can be done by 1-D IDCT on each row followed by 1-D IDCT
5166 * on each column. Direct algorithms are also available, but they are
5167 * much more complex and seem not to be any faster when reduced to code.
5168 *
5169 * The poop on this scaling stuff is as follows:
5170 *
5171 * Each 1-D IDCT step produces outputs which are a factor of sqrt(N)
5172 * larger than the true IDCT outputs. The final outputs are therefore
5173 * a factor of N larger than desired; since N=8 this can be cured by
5174 * a simple right shift at the end of the algorithm. The advantage of
5175 * this arrangement is that we save two multiplications per 1-D IDCT,
5176 * because the y0 and y4 inputs need not be divided by sqrt(N).
5177 *
5178 * We have to do addition and subtraction of the integer inputs, which
5179 * is no problem, and multiplication by fractional constants, which is
5180 * a problem to do in integer arithmetic. We multiply all the constants
5181 * by CONST_SCALE and convert them to integer constants (thus retaining
5182 * CONST_BITS bits of precision in the constants). After doing a
5183 * multiplication we have to divide the product by CONST_SCALE, with proper
5184 * rounding, to produce the correct output. This division can be done
5185 * cheaply as a right shift of CONST_BITS bits. We postpone shifting
5186 * as long as possible so that partial sums can be added together with
5187 * full fractional precision.
5188 *
5189 * The outputs of the first pass are scaled up by PASS1_BITS bits so that
5190 * they are represented to better-than-integral precision. These outputs
5191 * require BITS_IN_JSAMPLE + PASS1_BITS + 3 bits; this fits in a 16-bit word
5192 * with the recommended scaling. (To scale up 12-bit sample data further, an
5193 * intermediate int array would be needed.)
5194 *
5195 * To avoid overflow of the 32-bit intermediate results in pass 2, we must
5196 * have BITS_IN_JSAMPLE + CONST_BITS + PASS1_BITS <= 26. Error analysis
5197 * shows that the values given below are the most effective.
5198 */
5199
5200#ifdef EIGHT_BIT_SAMPLES
5201#define PASS1_BITS 2
5202#else
5203#define PASS1_BITS 1 /* lose a little precision to avoid overflow */
5204#endif
5205
5206#define ONE ((int) 1)
5207
5208#define CONST_SCALE (ONE << CONST_BITS)
5209
5210/* Convert a positive real constant to an integer scaled by CONST_SCALE.
5211 * IMPORTANT: if your compiler doesn't do this arithmetic at compile time,
5212 * you will pay a significant penalty in run time. In that case, figure
5213 * the correct integer constant values and insert them by hand.
5214 */
5215
5216#define FIX(x) ((int) ((x) * CONST_SCALE + 0.5))
5217
5218/* When adding two opposite-signed fixes, the 0.5 cancels */
5219#define FIX2(x) ((int) ((x) * CONST_SCALE))
5220
5221/* Descale and correctly round an int value that's scaled by N bits.
5222 * We assume RIGHT_SHIFT rounds towards minus infinity, so adding
5223 * the fudge factor is correct for either sign of X.
5224 */
5225
5226#define DESCALE(x,n) RIGHT_SHIFT((x) + (ONE << ((n)-1)), n)
5227
5228/* Multiply an int variable by an INT32 constant to yield an INT32 result.
5229 * For 8-bit samples with the recommended scaling, all the variable
5230 * and constant values involved are no more than 16 bits wide, so a
5231 * 16x16->32 bit multiply can be used instead of a full 32x32 multiply;
5232 * this provides a useful speedup on many machines.
5233 * There is no way to specify a 16x16->32 multiply in portable C, but
5234 * some C compilers will do the right thing if you provide the correct
5235 * combination of casts.
5236 * NB: for 12-bit samples, a full 32-bit multiplication will be needed.
5237 */
5238
5239#ifdef EIGHT_BIT_SAMPLES
5240#ifdef SHORTxSHORT_32 /* may work if 'int' is 32 bits */
5241#define MULTIPLY(var,const) (((INT16) (var)) * ((INT16) (const)))
5242#endif
5243#ifdef SHORTxLCONST_32 /* known to work with Microsoft C 6.0 */
5244#define MULTIPLY(var,const) (((INT16) (var)) * ((int) (const)))
5245#endif
5246#endif
5247
5248#define MULTIPLY(var,const) ((var) * (const))
5249
5250#define SPARSE_SCALE_FACTOR 8
5251
5252/* Precomputed idct value arrays. */
5253
5254static DCTELEM PreIDCT[64][64];
5255
5256
5257/*
5258 *--------------------------------------------------------------
5259 *
5260 * init_pre_idct --
5261 *
5262 * Pre-computes singleton coefficient IDCT values.
5263 *
5264 * Results:
5265 * None.
5266 *
5267 * Side effects:
5268 * None.
5269 *
5270 *--------------------------------------------------------------
5271 */
5272void
5273init_pre_idct() {
5274 int i;
5275
5276 for (i=0; i<64; i++) {
5277 memset((char *) PreIDCT[i], 0, 64*sizeof(DCTELEM));
5278 PreIDCT[i][i] = 1 << SPARSE_SCALE_FACTOR;
5279 j_rev_dct(PreIDCT[i]);
5280 }
5281}
5282
5283
5284/*
5285 *--------------------------------------------------------------
5286 *
5287 * j_rev_dct_sparse --
5288 *
5289 * Performs the inverse DCT on one block of coefficients.
5290 *
5291 * Results:
5292 * None.
5293 *
5294 * Side effects:
5295 * None.
5296 *
5297 *--------------------------------------------------------------
5298 */
5299
5300void
5301j_rev_dct_sparse (data, pos)
5302 DCTBLOCK data;
5303 int pos;
5304{
5305 short int val;
5306 register int *dp;
5307 register int v;
5308 int quant;
5309
5310#ifdef SPARSE_AC
5311 register DCTELEM *dataptr;
5312 DCTELEM *ndataptr;
5313 int coeff, rr;
5314 DCTBLOCK tmpdata, tmp2data;
5315 DCTELEM *tmpdataptr, *tmp2dataptr;
5316 int printFlag = 1;
5317#endif
5318
5319
5320 /* If DC Coefficient. */
5321
5322 if (pos == 0) {
5323 dp = (int *)data;
5324 v = *data;
5325 quant = 8;
5326
5327 /* Compute 32 bit value to assign. This speeds things up a bit */
5328 if (v < 0) {
5329 val = -v;
5330 val += (quant >> 1);
5331 val /= quant;
5332 val = -val;
5333 }
5334 else {
5335 val = (v + (quant >> 1)) / quant;
5336 }
5337
5338 v = ((val & 0xffff) | (val << 16));
5339
5340 dp[0] = v; dp[1] = v; dp[2] = v; dp[3] = v;
5341 dp[4] = v; dp[5] = v; dp[6] = v; dp[7] = v;
5342 dp[8] = v; dp[9] = v; dp[10] = v; dp[11] = v;
5343 dp[12] = v; dp[13] = v; dp[14] = v; dp[15] = v;
5344 dp[16] = v; dp[17] = v; dp[18] = v; dp[19] = v;
5345 dp[20] = v; dp[21] = v; dp[22] = v; dp[23] = v;
5346 dp[24] = v; dp[25] = v; dp[26] = v; dp[27] = v;
5347 dp[28] = v; dp[29] = v; dp[30] = v; dp[31] = v;
5348
5349 return;
5350 }
5351
5352 /* Some other coefficient. */
5353
5354#ifdef SPARSE_AC
5355 dataptr = (DCTELEM *)data;
5356 coeff = dataptr[pos];
5357 ndataptr = PreIDCT[pos];
5358
5359 printf ("␌\n");
5360 printf ("COEFFICIENT = %3d, POSITION = %2d\n", coeff, pos);
5361
5362 for (v=0; v<64; v++) {
5363 memcpy((char *) tmpdata, data, 64*sizeof(DCTELEM));
5364 }
5365 tmpdataptr = (DCTELEM *)tmpdata;
5366
5367 for (v=0; v<64; v++) {
5368 memcpy((char *) tmp2data, data, 64*sizeof(DCTELEM));
5369 }
5370 tmp2dataptr = (DCTELEM *)tmp2data;
5371
5372 for (rr=0; rr<4; rr++) {
5373 dataptr[0] = (ndataptr[0] * coeff) >> SPARSE_SCALE_FACTOR;
5374 dataptr[1] = (ndataptr[1] * coeff) >> SPARSE_SCALE_FACTOR;
5375 dataptr[2] = (ndataptr[2] * coeff) >> SPARSE_SCALE_FACTOR;
5376 dataptr[3] = (ndataptr[3] * coeff) >> SPARSE_SCALE_FACTOR;
5377 dataptr[4] = (ndataptr[4] * coeff) >> SPARSE_SCALE_FACTOR;
5378 dataptr[5] = (ndataptr[5] * coeff) >> SPARSE_SCALE_FACTOR;
5379 dataptr[6] = (ndataptr[6] * coeff) >> SPARSE_SCALE_FACTOR;
5380 dataptr[7] = (ndataptr[7] * coeff) >> SPARSE_SCALE_FACTOR;
5381 dataptr[8] = (ndataptr[8] * coeff) >> SPARSE_SCALE_FACTOR;
5382 dataptr[9] = (ndataptr[9] * coeff) >> SPARSE_SCALE_FACTOR;
5383 dataptr[10] = (ndataptr[10] * coeff) >> SPARSE_SCALE_FACTOR;
5384 dataptr[11] = (ndataptr[11] * coeff) >> SPARSE_SCALE_FACTOR;
5385 dataptr[12] = (ndataptr[12] * coeff) >> SPARSE_SCALE_FACTOR;
5386 dataptr[13] = (ndataptr[13] * coeff) >> SPARSE_SCALE_FACTOR;
5387 dataptr[14] = (ndataptr[14] * coeff) >> SPARSE_SCALE_FACTOR;
5388 dataptr[15] = (ndataptr[15] * coeff) >> SPARSE_SCALE_FACTOR;
5389 dataptr += 16;
5390 ndataptr += 16;
5391 }
5392
5393 dataptr = (DCTELEM *) data;
5394
5395#else /* NO_SPARSE_AC */
5396#ifdef FLOATDCT
5397 if (qualityFlag)
5398 float_idct(data);
5399 else
5400#endif /* FLOATDCT */
5401 j_rev_dct(data);
5402#endif /* SPARSE_AC */
5403 return;
5404
5405}
5406
5407
5408/*
5409 *--------------------------------------------------------------
5410 *
5411 * j_rev_dct --
5412 *
5413 * The inverse DCT function.
5414 *
5415 * Results:
5416 * None.
5417 *
5418 * Side effects:
5419 * None.
5420 *
5421 *--------------------------------------------------------------
5422 */
5423void
5424j_rev_dct (data)
5425 DCTBLOCK data;
5426{
5427 int tmp0, tmp1, tmp2, tmp3;
5428 int tmp10, tmp11, tmp12, tmp13;
5429 int z1, z2, z3, z4, z5;
5430 int d0, d1, d2, d3, d4, d5, d6, d7;
5431 register DCTELEM *dataptr;
5432 int rowctr;
5433 SHIFT_TEMPS
5434
5435 /* Pass 1: process rows. */
5436 /* Note results are scaled up by sqrt(8) compared to a true IDCT; */
5437 /* furthermore, we scale the results by 2**PASS1_BITS. */
5438
5439 dataptr = data;
5440
5441 for (rowctr = DCTSIZE-1; rowctr >= 0; rowctr--) {
5442 /* Due to quantization, we will usually find that many of the input
5443 * coefficients are zero, especially the AC terms. We can exploit this
5444 * by short-circuiting the IDCT calculation for any row in which all
5445 * the AC terms are zero. In that case each output is equal to the
5446 * DC coefficient (with scale factor as needed).
5447 * With typical images and quantization tables, half or more of the
5448 * row DCT calculations can be simplified this way.
5449 */
5450
5451 register int *idataptr = (int*)dataptr;
5452 d0 = dataptr[0];
5453 d1 = dataptr[1];
5454 if ((d1 == 0) && (idataptr[1] | idataptr[2] | idataptr[3]) == 0) {
5455 /* AC terms all zero */
5456 if (d0) {
5457 /* Compute a 32 bit value to assign. */
5458 DCTELEM dcval = (DCTELEM) (d0 << PASS1_BITS);
5459 register int v = (dcval & 0xffff) | (dcval << 16);
5460
5461 idataptr[0] = v;
5462 idataptr[1] = v;
5463 idataptr[2] = v;
5464 idataptr[3] = v;
5465 }
5466
5467 dataptr += DCTSIZE; /* advance pointer to next row */
5468 continue;
5469 }
5470 d2 = dataptr[2];
5471 d3 = dataptr[3];
5472 d4 = dataptr[4];
5473 d5 = dataptr[5];
5474 d6 = dataptr[6];
5475 d7 = dataptr[7];
5476
5477 /* Even part: reverse the even part of the forward DCT. */
5478 /* The rotator is sqrt(2)*c(-6). */
5479 if (d6) {
5480 if (d4) {
5481 if (d2) {
5482 if (d0) {
5483 /* d0 != 0, d2 != 0, d4 != 0, d6 != 0 */
5484 z1 = MULTIPLY(d2 + d6, FIX(0.541196100));
5485 tmp2 = z1 + MULTIPLY(d6, - FIX(1.847759065));
5486 tmp3 = z1 + MULTIPLY(d2, FIX(0.765366865));
5487
5488 tmp0 = (d0 + d4) << CONST_BITS;
5489 tmp1 = (d0 - d4) << CONST_BITS;
5490
5491 tmp10 = tmp0 + tmp3;
5492 tmp13 = tmp0 - tmp3;
5493 tmp11 = tmp1 + tmp2;
5494 tmp12 = tmp1 - tmp2;
5495 } else {
5496 /* d0 == 0, d2 != 0, d4 != 0, d6 != 0 */
5497 z1 = MULTIPLY(d2 + d6, FIX(0.541196100));
5498 tmp2 = z1 + MULTIPLY(d6, - FIX(1.847759065));
5499 tmp3 = z1 + MULTIPLY(d2, FIX(0.765366865));
5500
5501 tmp0 = d4 << CONST_BITS;
5502
5503 tmp10 = tmp0 + tmp3;
5504 tmp13 = tmp0 - tmp3;
5505 tmp11 = tmp2 - tmp0;
5506 tmp12 = -(tmp0 + tmp2);
5507 }
5508 } else {
5509 if (d0) {
5510 /* d0 != 0, d2 == 0, d4 != 0, d6 != 0 */
5511 tmp2 = MULTIPLY(d6, - FIX2(1.306562965));
5512 tmp3 = MULTIPLY(d6, FIX(0.541196100));
5513
5514 tmp0 = (d0 + d4) << CONST_BITS;
5515 tmp1 = (d0 - d4) << CONST_BITS;
5516
5517 tmp10 = tmp0 + tmp3;
5518 tmp13 = tmp0 - tmp3;
5519 tmp11 = tmp1 + tmp2;
5520 tmp12 = tmp1 - tmp2;
5521 } else {
5522 /* d0 == 0, d2 == 0, d4 != 0, d6 != 0 */
5523 tmp2 = MULTIPLY(d6, - FIX2(1.306562965));
5524 tmp3 = MULTIPLY(d6, FIX(0.541196100));
5525
5526 tmp0 = d4 << CONST_BITS;
5527
5528 tmp10 = tmp0 + tmp3;
5529 tmp13 = tmp0 - tmp3;
5530 tmp11 = tmp2 - tmp0;
5531 tmp12 = -(tmp0 + tmp2);
5532 }
5533 }
5534 } else {
5535 if (d2) {
5536 if (d0) {
5537 /* d0 != 0, d2 != 0, d4 == 0, d6 != 0 */
5538 z1 = MULTIPLY(d2 + d6, FIX(0.541196100));
5539 tmp2 = z1 + MULTIPLY(d6, - FIX(1.847759065));
5540 tmp3 = z1 + MULTIPLY(d2, FIX(0.765366865));
5541
5542 tmp0 = d0 << CONST_BITS;
5543
5544 tmp10 = tmp0 + tmp3;
5545 tmp13 = tmp0 - tmp3;
5546 tmp11 = tmp0 + tmp2;
5547 tmp12 = tmp0 - tmp2;
5548 } else {
5549 /* d0 == 0, d2 != 0, d4 == 0, d6 != 0 */
5550 z1 = MULTIPLY(d2 + d6, FIX(0.541196100));
5551 tmp2 = z1 + MULTIPLY(d6, - FIX(1.847759065));
5552 tmp3 = z1 + MULTIPLY(d2, FIX(0.765366865));
5553
5554 tmp10 = tmp3;
5555 tmp13 = -tmp3;
5556 tmp11 = tmp2;
5557 tmp12 = -tmp2;
5558 }
5559 } else {
5560 if (d0) {
5561 /* d0 != 0, d2 == 0, d4 == 0, d6 != 0 */
5562 tmp2 = MULTIPLY(d6, - FIX2(1.306562965));
5563 tmp3 = MULTIPLY(d6, FIX(0.541196100));
5564
5565 tmp0 = d0 << CONST_BITS;
5566
5567 tmp10 = tmp0 + tmp3;
5568 tmp13 = tmp0 - tmp3;
5569 tmp11 = tmp0 + tmp2;
5570 tmp12 = tmp0 - tmp2;
5571 } else {
5572 /* d0 == 0, d2 == 0, d4 == 0, d6 != 0 */
5573 tmp2 = MULTIPLY(d6, - FIX2(1.306562965));
5574 tmp3 = MULTIPLY(d6, FIX(0.541196100));
5575
5576 tmp10 = tmp3;
5577 tmp13 = -tmp3;
5578 tmp11 = tmp2;
5579 tmp12 = -tmp2;
5580 }
5581 }
5582 }
5583 } else {
5584 if (d4) {
5585 if (d2) {
5586 if (d0) {
5587 /* d0 != 0, d2 != 0, d4 != 0, d6 == 0 */
5588 tmp2 = MULTIPLY(d2, FIX(0.541196100));
5589 tmp3 = MULTIPLY(d2, (FIX(1.306562965) + .5));
5590
5591 tmp0 = (d0 + d4) << CONST_BITS;
5592 tmp1 = (d0 - d4) << CONST_BITS;
5593
5594 tmp10 = tmp0 + tmp3;
5595 tmp13 = tmp0 - tmp3;
5596 tmp11 = tmp1 + tmp2;
5597 tmp12 = tmp1 - tmp2;
5598 } else {
5599 /* d0 == 0, d2 != 0, d4 != 0, d6 == 0 */
5600 tmp2 = MULTIPLY(d2, FIX(0.541196100));
5601 tmp3 = MULTIPLY(d2, (FIX(1.306562965) + .5));
5602
5603 tmp0 = d4 << CONST_BITS;
5604
5605 tmp10 = tmp0 + tmp3;
5606 tmp13 = tmp0 - tmp3;
5607 tmp11 = tmp2 - tmp0;
5608 tmp12 = -(tmp0 + tmp2);
5609 }
5610 } else {
5611 if (d0) {
5612 /* d0 != 0, d2 == 0, d4 != 0, d6 == 0 */
5613 tmp10 = tmp13 = (d0 + d4) << CONST_BITS;
5614 tmp11 = tmp12 = (d0 - d4) << CONST_BITS;
5615 } else {
5616 /* d0 == 0, d2 == 0, d4 != 0, d6 == 0 */
5617 tmp10 = tmp13 = d4 << CONST_BITS;
5618 tmp11 = tmp12 = -tmp10;
5619 }
5620 }
5621 } else {
5622 if (d2) {
5623 if (d0) {
5624 /* d0 != 0, d2 != 0, d4 == 0, d6 == 0 */
5625 tmp2 = MULTIPLY(d2, FIX(0.541196100));
5626 tmp3 = MULTIPLY(d2, (FIX(1.306562965) + .5));
5627
5628 tmp0 = d0 << CONST_BITS;
5629
5630 tmp10 = tmp0 + tmp3;
5631 tmp13 = tmp0 - tmp3;
5632 tmp11 = tmp0 + tmp2;
5633 tmp12 = tmp0 - tmp2;
5634 } else {
5635 /* d0 == 0, d2 != 0, d4 == 0, d6 == 0 */
5636 tmp2 = MULTIPLY(d2, FIX(0.541196100));
5637 tmp3 = MULTIPLY(d2, (FIX(1.306562965) + .5));
5638
5639 tmp10 = tmp3;
5640 tmp13 = -tmp3;
5641 tmp11 = tmp2;
5642 tmp12 = -tmp2;
5643 }
5644 } else {
5645 if (d0) {
5646 /* d0 != 0, d2 == 0, d4 == 0, d6 == 0 */
5647 tmp10 = tmp13 = tmp11 = tmp12 = d0 << CONST_BITS;
5648 } else {
5649 /* d0 == 0, d2 == 0, d4 == 0, d6 == 0 */
5650 tmp10 = tmp13 = tmp11 = tmp12 = 0;
5651 }
5652 }
5653 }
5654 }
5655
5656
5657 /* Odd part per figure 8; the matrix is unitary and hence its
5658 * transpose is its inverse. i0..i3 are y7,y5,y3,y1 respectively.
5659 */
5660
5661 if (d7) {
5662 if (d5) {
5663 if (d3) {
5664 if (d1) {
5665 /* d1 != 0, d3 != 0, d5 != 0, d7 != 0 */
5666 z1 = d7 + d1;
5667 z2 = d5 + d3;
5668 z3 = d7 + d3;
5669 z4 = d5 + d1;
5670 z5 = MULTIPLY(z3 + z4, FIX(1.175875602));
5671
5672 tmp0 = MULTIPLY(d7, FIX(0.298631336));
5673 tmp1 = MULTIPLY(d5, FIX(2.053119869));
5674 tmp2 = MULTIPLY(d3, FIX(3.072711026));
5675 tmp3 = MULTIPLY(d1, FIX(1.501321110));
5676 z1 = MULTIPLY(z1, - FIX(0.899976223));
5677 z2 = MULTIPLY(z2, - FIX(2.562915447));
5678 z3 = MULTIPLY(z3, - FIX(1.961570560));
5679 z4 = MULTIPLY(z4, - FIX(0.390180644));
5680
5681 z3 += z5;
5682 z4 += z5;
5683
5684 tmp0 += z1 + z3;
5685 tmp1 += z2 + z4;
5686 tmp2 += z2 + z3;
5687 tmp3 += z1 + z4;
5688 } else {
5689 /* d1 == 0, d3 != 0, d5 != 0, d7 != 0 */
5690 z2 = d5 + d3;
5691 z3 = d7 + d3;
5692 z5 = MULTIPLY(z3 + d5, FIX(1.175875602));
5693
5694 tmp0 = MULTIPLY(d7, FIX(0.298631336));
5695 tmp1 = MULTIPLY(d5, FIX(2.053119869));
5696 tmp2 = MULTIPLY(d3, FIX(3.072711026));
5697 z1 = MULTIPLY(d7, - FIX(0.899976223));
5698 z2 = MULTIPLY(z2, - FIX(2.562915447));
5699 z3 = MULTIPLY(z3, - FIX(1.961570560));
5700 z4 = MULTIPLY(d5, - FIX(0.390180644));
5701
5702 z3 += z5;
5703 z4 += z5;
5704
5705 tmp0 += z1 + z3;
5706 tmp1 += z2 + z4;
5707 tmp2 += z2 + z3;
5708 tmp3 = z1 + z4;
5709 }
5710 } else {
5711 if (d1) {
5712 /* d1 != 0, d3 == 0, d5 != 0, d7 != 0 */
5713 z1 = d7 + d1;
5714 z4 = d5 + d1;
5715 z5 = MULTIPLY(d7 + z4, FIX(1.175875602));
5716
5717 tmp0 = MULTIPLY(d7, FIX(0.298631336));
5718 tmp1 = MULTIPLY(d5, FIX(2.053119869));
5719 tmp3 = MULTIPLY(d1, FIX(1.501321110));
5720 z1 = MULTIPLY(z1, - FIX(0.899976223));
5721 z2 = MULTIPLY(d5, - FIX(2.562915447));
5722 z3 = MULTIPLY(d7, - FIX(1.961570560));
5723 z4 = MULTIPLY(z4, - FIX(0.390180644));
5724
5725 z3 += z5;
5726 z4 += z5;
5727
5728 tmp0 += z1 + z3;
5729 tmp1 += z2 + z4;
5730 tmp2 = z2 + z3;
5731 tmp3 += z1 + z4;
5732 } else {
5733 /* d1 == 0, d3 == 0, d5 != 0, d7 != 0 */
5734 z5 = MULTIPLY(d7 + d5, FIX(1.175875602));
5735
5736 tmp0 = MULTIPLY(d7, - FIX2(0.601344887));
5737 tmp1 = MULTIPLY(d5, - FIX2(0.509795578));
5738 z1 = MULTIPLY(d7, - FIX(0.899976223));
5739 z3 = MULTIPLY(d7, - FIX(1.961570560));
5740 z2 = MULTIPLY(d5, - FIX(2.562915447));
5741 z4 = MULTIPLY(d5, - FIX(0.390180644));
5742
5743 z3 += z5;
5744 z4 += z5;
5745
5746 tmp0 += z3;
5747 tmp1 += z4;
5748 tmp2 = z2 + z3;
5749 tmp3 = z1 + z4;
5750 }
5751 }
5752 } else {
5753 if (d3) {
5754 if (d1) {
5755 /* d1 != 0, d3 != 0, d5 == 0, d7 != 0 */
5756 z1 = d7 + d1;
5757 z3 = d7 + d3;
5758 z5 = MULTIPLY(z3 + d1, FIX(1.175875602));
5759
5760 tmp0 = MULTIPLY(d7, FIX(0.298631336));
5761 tmp2 = MULTIPLY(d3, FIX(3.072711026));
5762 tmp3 = MULTIPLY(d1, FIX(1.501321110));
5763 z1 = MULTIPLY(z1, - FIX(0.899976223));
5764 z2 = MULTIPLY(d3, - FIX(2.562915447));
5765 z3 = MULTIPLY(z3, - FIX(1.961570560));
5766 z4 = MULTIPLY(d1, - FIX(0.390180644));
5767
5768 z3 += z5;
5769 z4 += z5;
5770
5771 tmp0 += z1 + z3;
5772 tmp1 = z2 + z4;
5773 tmp2 += z2 + z3;
5774 tmp3 += z1 + z4;
5775 } else {
5776 /* d1 == 0, d3 != 0, d5 == 0, d7 != 0 */
5777 z3 = d7 + d3;
5778 z5 = MULTIPLY(z3, FIX(1.175875602));
5779
5780 tmp0 = MULTIPLY(d7, - FIX2(0.601344887));
5781 tmp2 = MULTIPLY(d3, FIX(0.509795579));
5782 z1 = MULTIPLY(d7, - FIX(0.899976223));
5783 z2 = MULTIPLY(d3, - FIX(2.562915447));
5784 z3 = MULTIPLY(z3, - FIX2(0.785694958));
5785
5786 tmp0 += z3;
5787 tmp1 = z2 + z5;
5788 tmp2 += z3;
5789 tmp3 = z1 + z5;
5790 }
5791 } else {
5792 if (d1) {
5793 /* d1 != 0, d3 == 0, d5 == 0, d7 != 0 */
5794 z1 = d7 + d1;
5795 z5 = MULTIPLY(z1, FIX(1.175875602));
5796
5797 tmp0 = MULTIPLY(d7, - FIX2(1.662939224));
5798 tmp3 = MULTIPLY(d1, FIX2(1.111140466));
5799 z1 = MULTIPLY(z1, FIX2(0.275899379));
5800 z3 = MULTIPLY(d7, - FIX(1.961570560));
5801 z4 = MULTIPLY(d1, - FIX(0.390180644));
5802
5803 tmp0 += z1;
5804 tmp1 = z4 + z5;
5805 tmp2 = z3 + z5;
5806 tmp3 += z1;
5807 } else {
5808 /* d1 == 0, d3 == 0, d5 == 0, d7 != 0 */
5809 tmp0 = MULTIPLY(d7, - FIX2(1.387039845));
5810 tmp1 = MULTIPLY(d7, FIX(1.175875602));
5811 tmp2 = MULTIPLY(d7, - FIX2(0.785694958));
5812 tmp3 = MULTIPLY(d7, FIX2(0.275899379));
5813 }
5814 }
5815 }
5816 } else {
5817 if (d5) {
5818 if (d3) {
5819 if (d1) {
5820 /* d1 != 0, d3 != 0, d5 != 0, d7 == 0 */
5821 z2 = d5 + d3;
5822 z4 = d5 + d1;
5823 z5 = MULTIPLY(d3 + z4, FIX(1.175875602));
5824
5825 tmp1 = MULTIPLY(d5, FIX(2.053119869));
5826 tmp2 = MULTIPLY(d3, FIX(3.072711026));
5827 tmp3 = MULTIPLY(d1, FIX(1.501321110));
5828 z1 = MULTIPLY(d1, - FIX(0.899976223));
5829 z2 = MULTIPLY(z2, - FIX(2.562915447));
5830 z3 = MULTIPLY(d3, - FIX(1.961570560));
5831 z4 = MULTIPLY(z4, - FIX(0.390180644));
5832
5833 z3 += z5;
5834 z4 += z5;
5835
5836 tmp0 = z1 + z3;
5837 tmp1 += z2 + z4;
5838 tmp2 += z2 + z3;
5839 tmp3 += z1 + z4;
5840 } else {
5841 /* d1 == 0, d3 != 0, d5 != 0, d7 == 0 */
5842 z2 = d5 + d3;
5843 z5 = MULTIPLY(z2, FIX(1.175875602));
5844
5845 tmp1 = MULTIPLY(d5, FIX2(1.662939225));
5846 tmp2 = MULTIPLY(d3, FIX2(1.111140466));
5847 z2 = MULTIPLY(z2, - FIX2(1.387039845));
5848 z3 = MULTIPLY(d3, - FIX(1.961570560));
5849 z4 = MULTIPLY(d5, - FIX(0.390180644));
5850
5851 tmp0 = z3 + z5;
5852 tmp1 += z2;
5853 tmp2 += z2;
5854 tmp3 = z4 + z5;
5855 }
5856 } else {
5857 if (d1) {
5858 /* d1 != 0, d3 == 0, d5 != 0, d7 == 0 */
5859 z4 = d5 + d1;
5860 z5 = MULTIPLY(z4, FIX(1.175875602));
5861
5862 tmp1 = MULTIPLY(d5, - FIX2(0.509795578));
5863 tmp3 = MULTIPLY(d1, FIX2(0.601344887));
5864 z1 = MULTIPLY(d1, - FIX(0.899976223));
5865 z2 = MULTIPLY(d5, - FIX(2.562915447));
5866 z4 = MULTIPLY(z4, FIX2(0.785694958));
5867
5868 tmp0 = z1 + z5;
5869 tmp1 += z4;
5870 tmp2 = z2 + z5;
5871 tmp3 += z4;
5872 } else {
5873 /* d1 == 0, d3 == 0, d5 != 0, d7 == 0 */
5874 tmp0 = MULTIPLY(d5, FIX(1.175875602));
5875 tmp1 = MULTIPLY(d5, FIX2(0.275899380));
5876 tmp2 = MULTIPLY(d5, - FIX2(1.387039845));
5877 tmp3 = MULTIPLY(d5, FIX2(0.785694958));
5878 }
5879 }
5880 } else {
5881 if (d3) {
5882 if (d1) {
5883 /* d1 != 0, d3 != 0, d5 == 0, d7 == 0 */
5884 z5 = d3 + d1;
5885
5886 tmp2 = MULTIPLY(d3, - FIX(1.451774981));
5887 tmp3 = MULTIPLY(d1, (FIX(0.211164243) - 1));
5888 z1 = MULTIPLY(d1, FIX(1.061594337));
5889 z2 = MULTIPLY(d3, - FIX(2.172734803));
5890 z4 = MULTIPLY(z5, FIX(0.785694958));
5891 z5 = MULTIPLY(z5, FIX(1.175875602));
5892
5893 tmp0 = z1 - z4;
5894 tmp1 = z2 + z4;
5895 tmp2 += z5;
5896 tmp3 += z5;
5897 } else {
5898 /* d1 == 0, d3 != 0, d5 == 0, d7 == 0 */
5899 tmp0 = MULTIPLY(d3, - FIX2(0.785694958));
5900 tmp1 = MULTIPLY(d3, - FIX2(1.387039845));
5901 tmp2 = MULTIPLY(d3, - FIX2(0.275899379));
5902 tmp3 = MULTIPLY(d3, FIX(1.175875602));
5903 }
5904 } else {
5905 if (d1) {
5906 /* d1 != 0, d3 == 0, d5 == 0, d7 == 0 */
5907 tmp0 = MULTIPLY(d1, FIX2(0.275899379));
5908 tmp1 = MULTIPLY(d1, FIX2(0.785694958));
5909 tmp2 = MULTIPLY(d1, FIX(1.175875602));
5910 tmp3 = MULTIPLY(d1, FIX2(1.387039845));
5911 } else {
5912 /* d1 == 0, d3 == 0, d5 == 0, d7 == 0 */
5913 tmp0 = tmp1 = tmp2 = tmp3 = 0;
5914 }
5915 }
5916 }
5917 }
5918
5919 /* Final output stage: inputs are tmp10..tmp13, tmp0..tmp3 */
5920
5921 dataptr[0] = (DCTELEM) DESCALE(tmp10 + tmp3, CONST_BITS-PASS1_BITS);
5922 dataptr[7] = (DCTELEM) DESCALE(tmp10 - tmp3, CONST_BITS-PASS1_BITS);
5923 dataptr[1] = (DCTELEM) DESCALE(tmp11 + tmp2, CONST_BITS-PASS1_BITS);
5924 dataptr[6] = (DCTELEM) DESCALE(tmp11 - tmp2, CONST_BITS-PASS1_BITS);
5925 dataptr[2] = (DCTELEM) DESCALE(tmp12 + tmp1, CONST_BITS-PASS1_BITS);
5926 dataptr[5] = (DCTELEM) DESCALE(tmp12 - tmp1, CONST_BITS-PASS1_BITS);
5927 dataptr[3] = (DCTELEM) DESCALE(tmp13 + tmp0, CONST_BITS-PASS1_BITS);
5928 dataptr[4] = (DCTELEM) DESCALE(tmp13 - tmp0, CONST_BITS-PASS1_BITS);
5929
5930 dataptr += DCTSIZE; /* advance pointer to next row */
5931 }
5932
5933 /* Pass 2: process columns. */
5934 /* Note that we must descale the results by a factor of 8 == 2**3, */
5935 /* and also undo the PASS1_BITS scaling. */
5936
5937 dataptr = data;
5938 for (rowctr = DCTSIZE-1; rowctr >= 0; rowctr--) {
5939 /* Columns of zeroes can be exploited in the same way as we did with rows.
5940 * However, the row calculation has created many nonzero AC terms, so the
5941 * simplification applies less often (typically 5% to 10% of the time).
5942 * On machines with very fast multiplication, it's possible that the
5943 * test takes more time than it's worth. In that case this section
5944 * may be commented out.
5945 */
5946
5947 d0 = dataptr[DCTSIZE*0];
5948 d1 = dataptr[DCTSIZE*1];
5949 d2 = dataptr[DCTSIZE*2];
5950 d3 = dataptr[DCTSIZE*3];
5951 d4 = dataptr[DCTSIZE*4];
5952 d5 = dataptr[DCTSIZE*5];
5953 d6 = dataptr[DCTSIZE*6];
5954 d7 = dataptr[DCTSIZE*7];
5955
5956 /* Even part: reverse the even part of the forward DCT. */
5957 /* The rotator is sqrt(2)*c(-6). */
5958 if (d6) {
5959 if (d4) {
5960 if (d2) {
5961 if (d0) {
5962 /* d0 != 0, d2 != 0, d4 != 0, d6 != 0 */
5963 z1 = MULTIPLY(d2 + d6, FIX(0.541196100));
5964 tmp2 = z1 + MULTIPLY(d6, - FIX(1.847759065));
5965 tmp3 = z1 + MULTIPLY(d2, FIX(0.765366865));
5966
5967 tmp0 = (d0 + d4) << CONST_BITS;
5968 tmp1 = (d0 - d4) << CONST_BITS;
5969
5970 tmp10 = tmp0 + tmp3;
5971 tmp13 = tmp0 - tmp3;
5972 tmp11 = tmp1 + tmp2;
5973 tmp12 = tmp1 - tmp2;
5974 } else {
5975 /* d0 == 0, d2 != 0, d4 != 0, d6 != 0 */
5976 z1 = MULTIPLY(d2 + d6, FIX(0.541196100));
5977 tmp2 = z1 + MULTIPLY(d6, - FIX(1.847759065));
5978 tmp3 = z1 + MULTIPLY(d2, FIX(0.765366865));
5979
5980 tmp0 = d4 << CONST_BITS;
5981
5982 tmp10 = tmp0 + tmp3;
5983 tmp13 = tmp0 - tmp3;
5984 tmp11 = tmp2 - tmp0;
5985 tmp12 = -(tmp0 + tmp2);
5986 }
5987 } else {
5988 if (d0) {
5989 /* d0 != 0, d2 == 0, d4 != 0, d6 != 0 */
5990 tmp2 = MULTIPLY(d6, - FIX2(1.306562965));
5991 tmp3 = MULTIPLY(d6, FIX(0.541196100));
5992
5993 tmp0 = (d0 + d4) << CONST_BITS;
5994 tmp1 = (d0 - d4) << CONST_BITS;
5995
5996 tmp10 = tmp0 + tmp3;
5997 tmp13 = tmp0 - tmp3;
5998 tmp11 = tmp1 + tmp2;
5999 tmp12 = tmp1 - tmp2;
6000 } else {
6001 /* d0 == 0, d2 == 0, d4 != 0, d6 != 0 */
6002 tmp2 = MULTIPLY(d6, -FIX2(1.306562965));
6003 tmp3 = MULTIPLY(d6, FIX(0.541196100));
6004
6005 tmp0 = d4 << CONST_BITS;
6006
6007 tmp10 = tmp0 + tmp3;
6008 tmp13 = tmp0 - tmp3;
6009 tmp11 = tmp2 - tmp0;
6010 tmp12 = -(tmp0 + tmp2);
6011 }
6012 }
6013 } else {
6014 if (d2) {
6015 if (d0) {
6016 /* d0 != 0, d2 != 0, d4 == 0, d6 != 0 */
6017 z1 = MULTIPLY(d2 + d6, FIX(0.541196100));
6018 tmp2 = z1 + MULTIPLY(d6, - FIX(1.847759065));
6019 tmp3 = z1 + MULTIPLY(d2, FIX(0.765366865));
6020
6021 tmp0 = d0 << CONST_BITS;
6022
6023 tmp10 = tmp0 + tmp3;
6024 tmp13 = tmp0 - tmp3;
6025 tmp11 = tmp0 + tmp2;
6026 tmp12 = tmp0 - tmp2;
6027 } else {
6028 /* d0 == 0, d2 != 0, d4 == 0, d6 != 0 */
6029 z1 = MULTIPLY(d2 + d6, FIX(0.541196100));
6030 tmp2 = z1 + MULTIPLY(d6, - FIX(1.847759065));
6031 tmp3 = z1 + MULTIPLY(d2, FIX(0.765366865));
6032
6033 tmp10 = tmp3;
6034 tmp13 = -tmp3;
6035 tmp11 = tmp2;
6036 tmp12 = -tmp2;
6037 }
6038 } else {
6039 if (d0) {
6040 /* d0 != 0, d2 == 0, d4 == 0, d6 != 0 */
6041 tmp2 = MULTIPLY(d6, - FIX2(1.306562965));
6042 tmp3 = MULTIPLY(d6, FIX(0.541196100));
6043
6044 tmp0 = d0 << CONST_BITS;
6045
6046 tmp10 = tmp0 + tmp3;
6047 tmp13 = tmp0 - tmp3;
6048 tmp11 = tmp0 + tmp2;
6049 tmp12 = tmp0 - tmp2;
6050 } else {
6051 /* d0 == 0, d2 == 0, d4 == 0, d6 != 0 */
6052 tmp2 = MULTIPLY(d6, - FIX2(1.306562965));
6053 tmp3 = MULTIPLY(d6, FIX(0.541196100));
6054
6055 tmp10 = tmp3;
6056 tmp13 = -tmp3;
6057 tmp11 = tmp2;
6058 tmp12 = -tmp2;
6059 }
6060 }
6061 }
6062 } else {
6063 if (d4) {
6064 if (d2) {
6065 if (d0) {
6066 /* d0 != 0, d2 != 0, d4 != 0, d6 == 0 */
6067 tmp2 = MULTIPLY(d2, FIX(0.541196100));
6068 tmp3 = MULTIPLY(d2, (FIX(1.306562965) + .5));
6069
6070 tmp0 = (d0 + d4) << CONST_BITS;
6071 tmp1 = (d0 - d4) << CONST_BITS;
6072
6073 tmp10 = tmp0 + tmp3;
6074 tmp13 = tmp0 - tmp3;
6075 tmp11 = tmp1 + tmp2;
6076 tmp12 = tmp1 - tmp2;
6077 } else {
6078 /* d0 == 0, d2 != 0, d4 != 0, d6 == 0 */
6079 tmp2 = MULTIPLY(d2, FIX(0.541196100));
6080 tmp3 = MULTIPLY(d2, (FIX(1.306562965) + .5));
6081
6082 tmp0 = d4 << CONST_BITS;
6083
6084 tmp10 = tmp0 + tmp3;
6085 tmp13 = tmp0 - tmp3;
6086 tmp11 = tmp2 - tmp0;
6087 tmp12 = -(tmp0 + tmp2);
6088 }
6089 } else {
6090 if (d0) {
6091 /* d0 != 0, d2 == 0, d4 != 0, d6 == 0 */
6092 tmp10 = tmp13 = (d0 + d4) << CONST_BITS;
6093 tmp11 = tmp12 = (d0 - d4) << CONST_BITS;
6094 } else {
6095 /* d0 == 0, d2 == 0, d4 != 0, d6 == 0 */
6096 tmp10 = tmp13 = d4 << CONST_BITS;
6097 tmp11 = tmp12 = -tmp10;
6098 }
6099 }
6100 } else {
6101 if (d2) {
6102 if (d0) {
6103 /* d0 != 0, d2 != 0, d4 == 0, d6 == 0 */
6104 tmp2 = MULTIPLY(d2, FIX(0.541196100));
6105 tmp3 = MULTIPLY(d2, (FIX(1.306562965) + .5));
6106
6107 tmp0 = d0 << CONST_BITS;
6108
6109 tmp10 = tmp0 + tmp3;
6110 tmp13 = tmp0 - tmp3;
6111 tmp11 = tmp0 + tmp2;
6112 tmp12 = tmp0 - tmp2;
6113 } else {
6114 /* d0 == 0, d2 != 0, d4 == 0, d6 == 0 */
6115 tmp2 = MULTIPLY(d2, FIX(0.541196100));
6116 tmp3 = MULTIPLY(d2, (FIX(1.306562965) + .5));
6117
6118 tmp10 = tmp3;
6119 tmp13 = -tmp3;
6120 tmp11 = tmp2;
6121 tmp12 = -tmp2;
6122 }
6123 } else {
6124 if (d0) {
6125 /* d0 != 0, d2 == 0, d4 == 0, d6 == 0 */
6126 tmp10 = tmp13 = tmp11 = tmp12 = d0 << CONST_BITS;
6127 } else {
6128 /* d0 == 0, d2 == 0, d4 == 0, d6 == 0 */
6129 tmp10 = tmp13 = tmp11 = tmp12 = 0;
6130 }
6131 }
6132 }
6133 }
6134
6135 /* Odd part per figure 8; the matrix is unitary and hence its
6136 * transpose is its inverse. i0..i3 are y7,y5,y3,y1 respectively.
6137 */
6138 if (d7) {
6139 if (d5) {
6140 if (d3) {
6141 if (d1) {
6142 /* d1 != 0, d3 != 0, d5 != 0, d7 != 0 */
6143 z1 = d7 + d1;
6144 z2 = d5 + d3;
6145 z3 = d7 + d3;
6146 z4 = d5 + d1;
6147 z5 = MULTIPLY(z3 + z4, FIX(1.175875602));
6148
6149 tmp0 = MULTIPLY(d7, FIX(0.298631336));
6150 tmp1 = MULTIPLY(d5, FIX(2.053119869));
6151 tmp2 = MULTIPLY(d3, FIX(3.072711026));
6152 tmp3 = MULTIPLY(d1, FIX(1.501321110));
6153 z1 = MULTIPLY(z1, - FIX(0.899976223));
6154 z2 = MULTIPLY(z2, - FIX(2.562915447));
6155 z3 = MULTIPLY(z3, - FIX(1.961570560));
6156 z4 = MULTIPLY(z4, - FIX(0.390180644));
6157
6158 z3 += z5;
6159 z4 += z5;
6160
6161 tmp0 += z1 + z3;
6162 tmp1 += z2 + z4;
6163 tmp2 += z2 + z3;
6164 tmp3 += z1 + z4;
6165 } else {
6166 /* d1 == 0, d3 != 0, d5 != 0, d7 != 0 */
6167 z2 = d5 + d3;
6168 z3 = d7 + d3;
6169 z5 = MULTIPLY(z3 + d5, FIX(1.175875602));
6170
6171 tmp0 = MULTIPLY(d7, FIX(0.298631336));
6172 tmp1 = MULTIPLY(d5, FIX(2.053119869));
6173 tmp2 = MULTIPLY(d3, FIX(3.072711026));
6174 z1 = MULTIPLY(d7, - FIX(0.899976223));
6175 z2 = MULTIPLY(z2, - FIX(2.562915447));
6176 z3 = MULTIPLY(z3, - FIX(1.961570560));
6177 z4 = MULTIPLY(d5, - FIX(0.390180644));
6178
6179 z3 += z5;
6180 z4 += z5;
6181
6182 tmp0 += z1 + z3;
6183 tmp1 += z2 + z4;
6184 tmp2 += z2 + z3;
6185 tmp3 = z1 + z4;
6186 }
6187 } else {
6188 if (d1) {
6189 /* d1 != 0, d3 == 0, d5 != 0, d7 != 0 */
6190 z1 = d7 + d1;
6191 z4 = d5 + d1;
6192 z5 = MULTIPLY(d7 + z4, FIX(1.175875602));
6193
6194 tmp0 = MULTIPLY(d7, FIX(0.298631336));
6195 tmp1 = MULTIPLY(d5, FIX(2.053119869));
6196 tmp3 = MULTIPLY(d1, FIX(1.501321110));
6197 z1 = MULTIPLY(z1, - FIX(0.899976223));
6198 z2 = MULTIPLY(d5, - FIX(2.562915447));
6199 z3 = MULTIPLY(d7, - FIX(1.961570560));
6200 z4 = MULTIPLY(z4, - FIX(0.390180644));
6201
6202 z3 += z5;
6203 z4 += z5;
6204
6205 tmp0 += z1 + z3;
6206 tmp1 += z2 + z4;
6207 tmp2 = z2 + z3;
6208 tmp3 += z1 + z4;
6209 } else {
6210 /* d1 == 0, d3 == 0, d5 != 0, d7 != 0 */
6211 z5 = MULTIPLY(d5 + d7, FIX(1.175875602));
6212
6213 tmp0 = MULTIPLY(d7, - FIX2(0.601344887));
6214 tmp1 = MULTIPLY(d5, - FIX2(0.509795578));
6215 z1 = MULTIPLY(d7, - FIX(0.899976223));
6216 z3 = MULTIPLY(d7, - FIX(1.961570560));
6217 z2 = MULTIPLY(d5, - FIX(2.562915447));
6218 z4 = MULTIPLY(d5, - FIX(0.390180644));
6219
6220 z3 += z5;
6221 z4 += z5;
6222
6223 tmp0 += z3;
6224 tmp1 += z4;
6225 tmp2 = z2 + z3;
6226 tmp3 = z1 + z4;
6227 }
6228 }
6229 } else {
6230 if (d3) {
6231 if (d1) {
6232 /* d1 != 0, d3 != 0, d5 == 0, d7 != 0 */
6233 z1 = d7 + d1;
6234 z3 = d7 + d3;
6235 z5 = MULTIPLY(z3 + d1, FIX(1.175875602));
6236
6237 tmp0 = MULTIPLY(d7, FIX(0.298631336));
6238 tmp2 = MULTIPLY(d3, FIX(3.072711026));
6239 tmp3 = MULTIPLY(d1, FIX(1.501321110));
6240 z1 = MULTIPLY(z1, - FIX(0.899976223));
6241 z2 = MULTIPLY(d3, - FIX(2.562915447));
6242 z3 = MULTIPLY(z3, - FIX(1.961570560));
6243 z4 = MULTIPLY(d1, - FIX(0.390180644));
6244
6245 z3 += z5;
6246 z4 += z5;
6247
6248 tmp0 += z1 + z3;
6249 tmp1 = z2 + z4;
6250 tmp2 += z2 + z3;
6251 tmp3 += z1 + z4;
6252 } else {
6253 /* d1 == 0, d3 != 0, d5 == 0, d7 != 0 */
6254 z3 = d7 + d3;
6255 z5 = MULTIPLY(z3, FIX(1.175875602));
6256
6257 tmp0 = MULTIPLY(d7, - FIX2(0.601344887));
6258 z1 = MULTIPLY(d7, - FIX(0.899976223));
6259 tmp2 = MULTIPLY(d3, FIX(0.509795579));
6260 z2 = MULTIPLY(d3, - FIX(2.562915447));
6261 z3 = MULTIPLY(z3, - FIX2(0.785694958));
6262
6263 tmp0 += z3;
6264 tmp1 = z2 + z5;
6265 tmp2 += z3;
6266 tmp3 = z1 + z5;
6267 }
6268 } else {
6269 if (d1) {
6270 /* d1 != 0, d3 == 0, d5 == 0, d7 != 0 */
6271 z1 = d7 + d1;
6272 z5 = MULTIPLY(z1, FIX(1.175875602));
6273
6274 tmp0 = MULTIPLY(d7, - FIX2(1.662939224));
6275 tmp3 = MULTIPLY(d1, FIX2(1.111140466));
6276 z1 = MULTIPLY(z1, FIX2(0.275899379));
6277 z3 = MULTIPLY(d7, - FIX(1.961570560));
6278 z4 = MULTIPLY(d1, - FIX(0.390180644));
6279
6280 tmp0 += z1;
6281 tmp1 = z4 + z5;
6282 tmp2 = z3 + z5;
6283 tmp3 += z1;
6284 } else {
6285 /* d1 == 0, d3 == 0, d5 == 0, d7 != 0 */
6286 tmp0 = MULTIPLY(d7, - FIX2(1.387039845));
6287 tmp1 = MULTIPLY(d7, FIX(1.175875602));
6288 tmp2 = MULTIPLY(d7, - FIX2(0.785694958));
6289 tmp3 = MULTIPLY(d7, FIX2(0.275899379));
6290 }
6291 }
6292 }
6293 } else {
6294 if (d5) {
6295 if (d3) {
6296 if (d1) {
6297 /* d1 != 0, d3 != 0, d5 != 0, d7 == 0 */
6298 z2 = d5 + d3;
6299 z4 = d5 + d1;
6300 z5 = MULTIPLY(d3 + z4, FIX(1.175875602));
6301
6302 tmp1 = MULTIPLY(d5, FIX(2.053119869));
6303 tmp2 = MULTIPLY(d3, FIX(3.072711026));
6304 tmp3 = MULTIPLY(d1, FIX(1.501321110));
6305 z1 = MULTIPLY(d1, - FIX(0.899976223));
6306 z2 = MULTIPLY(z2, - FIX(2.562915447));
6307 z3 = MULTIPLY(d3, - FIX(1.961570560));
6308 z4 = MULTIPLY(z4, - FIX(0.390180644));
6309
6310 z3 += z5;
6311 z4 += z5;
6312
6313 tmp0 = z1 + z3;
6314 tmp1 += z2 + z4;
6315 tmp2 += z2 + z3;
6316 tmp3 += z1 + z4;
6317 } else {
6318 /* d1 == 0, d3 != 0, d5 != 0, d7 == 0 */
6319 z2 = d5 + d3;
6320 z5 = MULTIPLY(z2, FIX(1.175875602));
6321
6322 tmp1 = MULTIPLY(d5, FIX2(1.662939225));
6323 tmp2 = MULTIPLY(d3, FIX2(1.111140466));
6324 z2 = MULTIPLY(z2, - FIX2(1.387039845));
6325 z3 = MULTIPLY(d3, - FIX(1.961570560));
6326 z4 = MULTIPLY(d5, - FIX(0.390180644));
6327
6328 tmp0 = z3 + z5;
6329 tmp1 += z2;
6330 tmp2 += z2;
6331 tmp3 = z4 + z5;
6332 }
6333 } else {
6334 if (d1) {
6335 /* d1 != 0, d3 == 0, d5 != 0, d7 == 0 */
6336 z4 = d5 + d1;
6337 z5 = MULTIPLY(z4, FIX(1.175875602));
6338
6339 tmp1 = MULTIPLY(d5, - FIX2(0.509795578));
6340 tmp3 = MULTIPLY(d1, FIX2(0.601344887));
6341 z1 = MULTIPLY(d1, - FIX(0.899976223));
6342 z2 = MULTIPLY(d5, - FIX(2.562915447));
6343 z4 = MULTIPLY(z4, FIX2(0.785694958));
6344
6345 tmp0 = z1 + z5;
6346 tmp1 += z4;
6347 tmp2 = z2 + z5;
6348 tmp3 += z4;
6349 } else {
6350 /* d1 == 0, d3 == 0, d5 != 0, d7 == 0 */
6351 tmp0 = MULTIPLY(d5, FIX(1.175875602));
6352 tmp1 = MULTIPLY(d5, FIX2(0.275899380));
6353 tmp2 = MULTIPLY(d5, - FIX2(1.387039845));
6354 tmp3 = MULTIPLY(d5, FIX2(0.785694958));
6355 }
6356 }
6357 } else {
6358 if (d3) {
6359 if (d1) {
6360 /* d1 != 0, d3 != 0, d5 == 0, d7 == 0 */
6361 z5 = d3 + d1;
6362
6363 tmp2 = MULTIPLY(d3, - FIX(1.451774981));
6364 tmp3 = MULTIPLY(d1, (FIX(0.211164243) - 1));
6365 z1 = MULTIPLY(d1, FIX(1.061594337));
6366 z2 = MULTIPLY(d3, - FIX(2.172734803));
6367 z4 = MULTIPLY(z5, FIX(0.785694958));
6368 z5 = MULTIPLY(z5, FIX(1.175875602));
6369
6370 tmp0 = z1 - z4;
6371 tmp1 = z2 + z4;
6372 tmp2 += z5;
6373 tmp3 += z5;
6374 } else {
6375 /* d1 == 0, d3 != 0, d5 == 0, d7 == 0 */
6376 tmp0 = MULTIPLY(d3, - FIX2(0.785694958));
6377 tmp1 = MULTIPLY(d3, - FIX2(1.387039845));
6378 tmp2 = MULTIPLY(d3, - FIX2(0.275899379));
6379 tmp3 = MULTIPLY(d3, FIX(1.175875602));
6380 }
6381 } else {
6382 if (d1) {
6383 /* d1 != 0, d3 == 0, d5 == 0, d7 == 0 */
6384 tmp0 = MULTIPLY(d1, FIX2(0.275899379));
6385 tmp1 = MULTIPLY(d1, FIX2(0.785694958));
6386 tmp2 = MULTIPLY(d1, FIX(1.175875602));
6387 tmp3 = MULTIPLY(d1, FIX2(1.387039845));
6388 } else {
6389 /* d1 == 0, d3 == 0, d5 == 0, d7 == 0 */
6390 tmp0 = tmp1 = tmp2 = tmp3 = 0;
6391 }
6392 }
6393 }
6394 }
6395
6396 /* Final output stage: inputs are tmp10..tmp13, tmp0..tmp3 */
6397
6398 dataptr[DCTSIZE*0] = (DCTELEM) DESCALE(tmp10 + tmp3,
6399 CONST_BITS+PASS1_BITS+3);
6400 dataptr[DCTSIZE*7] = (DCTELEM) DESCALE(tmp10 - tmp3,
6401 CONST_BITS+PASS1_BITS+3);
6402 dataptr[DCTSIZE*1] = (DCTELEM) DESCALE(tmp11 + tmp2,
6403 CONST_BITS+PASS1_BITS+3);
6404 dataptr[DCTSIZE*6] = (DCTELEM) DESCALE(tmp11 - tmp2,
6405 CONST_BITS+PASS1_BITS+3);
6406 dataptr[DCTSIZE*2] = (DCTELEM) DESCALE(tmp12 + tmp1,
6407 CONST_BITS+PASS1_BITS+3);
6408 dataptr[DCTSIZE*5] = (DCTELEM) DESCALE(tmp12 - tmp1,
6409 CONST_BITS+PASS1_BITS+3);
6410 dataptr[DCTSIZE*3] = (DCTELEM) DESCALE(tmp13 + tmp0,
6411 CONST_BITS+PASS1_BITS+3);
6412 dataptr[DCTSIZE*4] = (DCTELEM) DESCALE(tmp13 - tmp0,
6413 CONST_BITS+PASS1_BITS+3);
6414
6415 dataptr++; /* advance pointer to next column */
6416 }
6417}
6418
6419
6420/*
6421 *--------------------------------------------------------------
6422 *
6423 * ComputeVector --
6424 *
6425 * Computes motion vector given parameters previously parsed
6426 * and reconstructed.
6427 *
6428 * Results:
6429 * Reconstructed motion vector info is put into recon_* parameters
6430 * passed to this function. Also updated previous motion vector
6431 * information.
6432 *
6433 * Side effects:
6434 * None.
6435 *
6436 *--------------------------------------------------------------
6437 */
6438
6439#define ComputeVector(recon_right_ptr, recon_down_ptr, recon_right_prev, recon_down_prev, f, full_pel_vector, motion_h_code, motion_v_code, motion_h_r, motion_v_r) \
6440 \
6441{ \
6442 int comp_h_r, comp_v_r; \
6443 int right_little, right_big, down_little, down_big; \
6444 int max, min, new_vector; \
6445 \
6446 /* The following procedure for the reconstruction of motion vectors \
6447 is a direct and simple implementation of the instructions given \
6448 in the mpeg December 1991 standard draft. \
6449 */ \
6450 \
6451 if (f == 1 || motion_h_code == 0) \
6452 comp_h_r = 0; \
6453 else \
6454 comp_h_r = f - 1 - motion_h_r; \
6455 \
6456 if (f == 1 || motion_v_code == 0) \
6457 comp_v_r = 0; \
6458 else \
6459 comp_v_r = f - 1 - motion_v_r; \
6460 \
6461 right_little = motion_h_code * f; \
6462 if (right_little == 0) \
6463 right_big = 0; \
6464 else { \
6465 if (right_little > 0) { \
6466 right_little = right_little - comp_h_r; \
6467 right_big = right_little - 32 * f; \
6468 } \
6469 else { \
6470 right_little = right_little + comp_h_r; \
6471 right_big = right_little + 32 * f; \
6472 } \
6473 } \
6474 \
6475 down_little = motion_v_code * f; \
6476 if (down_little == 0) \
6477 down_big = 0; \
6478 else { \
6479 if (down_little > 0) { \
6480 down_little = down_little - comp_v_r; \
6481 down_big = down_little - 32 * f; \
6482 } \
6483 else { \
6484 down_little = down_little + comp_v_r; \
6485 down_big = down_little + 32 * f; \
6486 } \
6487 } \
6488 \
6489 max = 16 * f - 1; \
6490 min = -16 * f; \
6491 \
6492 new_vector = recon_right_prev + right_little; \
6493 \
6494 if (new_vector <= max && new_vector >= min) \
6495 *recon_right_ptr = recon_right_prev + right_little; \
6496 /* just new_vector */ \
6497 else \
6498 *recon_right_ptr = recon_right_prev + right_big; \
6499 recon_right_prev = *recon_right_ptr; \
6500 if (full_pel_vector) \
6501 *recon_right_ptr = *recon_right_ptr << 1; \
6502 \
6503 new_vector = recon_down_prev + down_little; \
6504 if (new_vector <= max && new_vector >= min) \
6505 *recon_down_ptr = recon_down_prev + down_little; \
6506 /* just new_vector */ \
6507 else \
6508 *recon_down_ptr = recon_down_prev + down_big; \
6509 recon_down_prev = *recon_down_ptr; \
6510 if (full_pel_vector) \
6511 *recon_down_ptr = *recon_down_ptr << 1; \
6512}
6513
6514
6515/*
6516 *--------------------------------------------------------------
6517 *
6518 * ComputeForwVector --
6519 *
6520 * Computes forward motion vector by calling ComputeVector
6521 * with appropriate parameters.
6522 *
6523 * Results:
6524 * Reconstructed motion vector placed in recon_right_for_ptr and
6525 * recon_down_for_ptr.
6526 *
6527 * Side effects:
6528 * None.
6529 *
6530 *--------------------------------------------------------------
6531 */
6532
6533void
6534ComputeForwVector(recon_right_for_ptr, recon_down_for_ptr, the_stream)
6535 int *recon_right_for_ptr;
6536 int *recon_down_for_ptr;
6537 mpeg_VidStream *the_stream;
6538{
6539
6540 Pict *picture;
6541 Macroblock *mblock;
6542
6543 picture = &(the_stream->picture);
6544 mblock = &(the_stream->mblock);
6545
6546 ComputeVector(recon_right_for_ptr, recon_down_for_ptr,
6547 mblock->recon_right_for_prev,
6548 mblock->recon_down_for_prev,
6549 (int) picture->forw_f,
6550 picture->full_pel_forw_vector,
6551 mblock->motion_h_forw_code, mblock->motion_v_forw_code,
6552 mblock->motion_h_forw_r, mblock->motion_v_forw_r);
6553}
6554
6555
6556/*
6557 *--------------------------------------------------------------
6558 *
6559 * ComputeBackVector --
6560 *
6561 * Computes backward motion vector by calling ComputeVector
6562 * with appropriate parameters.
6563 *
6564 * Results:
6565 * Reconstructed motion vector placed in recon_right_back_ptr and
6566 * recon_down_back_ptr.
6567 *
6568 * Side effects:
6569 * None.
6570 *
6571 *--------------------------------------------------------------
6572 */
6573
6574void
6575ComputeBackVector(recon_right_back_ptr, recon_down_back_ptr, the_stream)
6576 int *recon_right_back_ptr;
6577 int *recon_down_back_ptr;
6578 mpeg_VidStream *the_stream;
6579{
6580 Pict *picture;
6581 Macroblock *mblock;
6582
6583 picture = &(the_stream->picture);
6584 mblock = &(the_stream->mblock);
6585
6586 ComputeVector(recon_right_back_ptr, recon_down_back_ptr,
6587 mblock->recon_right_back_prev,
6588 mblock->recon_down_back_prev,
6589 (int) picture->back_f,
6590 picture->full_pel_back_vector,
6591 mblock->motion_h_back_code, mblock->motion_v_back_code,
6592 mblock->motion_h_back_r, mblock->motion_v_back_r);
6593
6594}
6595
6596
6597
6598/* Silly Constants.... */
6599#define PACK_START_CODE ((unsigned int)0x000001ba)
6600#define SYSTEM_HEADER_START_CODE ((unsigned int)0x000001bb)
6601#define PACKET_START_CODE_MASK ((unsigned int)0xffffff00)
6602#define PACKET_START_CODE_PREFIX ((unsigned int)0x00000100)
6603#define ISO_11172_END_CODE ((unsigned int)0x000001b9)
6604
6605#define PACK_HEADER_SIZE 8
6606
6607#define STD_AUDIO_STREAM_ID ((unsigned char) 0xb8)
6608#define STD_VIDEO_STREAM_ID ((unsigned char) 0xb9)
6609#define MIN_STREAM_ID_ID ((unsigned char) 0xbc)
6610#define RESERVED_STREAM_ID ((unsigned char) 0xbc)
6611#define PRIVATE_STREAM_1_ID ((unsigned char) 0xbd)
6612#define PADDING_STREAM_ID ((unsigned char) 0xbe)
6613#define PRIVATE_STREAM_2_ID ((unsigned char) 0xbf)
6614
6615#define STD_SYSTEM_CLOCK_FREQ (unsigned long)90000
6616#define MUX_RATE_SCALE_FACTOR 50
6617#define MAX_STREAMS 8
6618#define NOT_PACKET_ID ((unsigned char) 0xff)
6619#define KILL_BUFFER ((unsigned char) 0xfe)
6620
6621
6622/*
6623 *--------------------------------------------------------------
6624 *
6625 * get_more_data --
6626 *
6627 * Called by get_more_data to read in more data from
6628 * video MPG files (non-system-layer)
6629 *
6630 * Results:
6631 * Input buffer updated, buffer length updated.
6632 * Returns 1 if data read, 0 if EOF, -1 if error.
6633 *
6634 * Side effects:
6635 * None.
6636 *
6637 *--------------------------------------------------------------
6638 */
6639int
6640get_more_data(vid_stream)
6642{
6643 unsigned int **bs_ptr=&vid_stream->buf_start;
6644 int *max_length=&vid_stream->max_buf_length;
6645 int *length_ptr=&vid_stream->buf_length;
6646 unsigned int **buf_ptr=&vid_stream->buffer;
6647 int ioBytes, data, result;
6648 unsigned char byte;
6649 unsigned int *mark;
6650 int sys_layer= vid_stream->sys_layer;
6651
6652 if (sys_layer == 0) {
6653 return pure_get_more_data(*bs_ptr,
6654 *max_length,
6655 length_ptr,
6656 buf_ptr,
6657 vid_stream);
6658 }
6659
6660 if (sys_layer == -1) {
6661 /* Time to init ourselves */
6662 vid_stream->swap = (fw_htonl(1) != 1);
6663 mark = *bs_ptr;
6664 ioBytes = fread(&data, 1, 4, vid_stream->input);
6665
6666 if (ioBytes != 4) {
6667 return 0;
6668 }
6669
6670 data = fw_ntohl(data);
6671 if ( (data == PACK_START_CODE) || (data == SYSTEM_HEADER_START_CODE) ) {
6672 got_sys:
6673 /* Yow, a System Layer Stream. Much harder to parse. Call in the
6674 specialist.... */
6675 //fprintf(stderr,"This is an MPEG System Layer Stream. ");
6676 //fprintf(stderr,"Audio is not played.\n");
6677 vid_stream->sys_layer = 1;
6678 result = read_sys(vid_stream,(unsigned int) data);
6679 return result;
6680 } else if (data == SEQ_START_CODE) {
6681 got_seq:
6682 /* No system Layer junk, just pretent we didn't peek,
6683 and hereafter just call pure_get_more_data */
6684 vid_stream->sys_layer = 0;
6685 **bs_ptr = data;
6686 *length_ptr = 1;
6687 result = pure_get_more_data(*bs_ptr, *max_length,
6688 length_ptr, buf_ptr, vid_stream);
6689 *buf_ptr = *bs_ptr;
6690 return result;
6691 } else {
6692 int state;
6693
6694 fprintf(stderr, "Junk at start of stream, searching for start code\n");
6695 state = 0;
6696 while (TRUE) {
6697 if ((ioBytes = fread(&byte, 1, 1, vid_stream->input)) != 1) return 0;
6698 if (byte == 0) {
6699 if (state < 2) state++;
6700 } else if ((byte == 1) && (state == 2)) {
6701 state++;
6702 } else {
6703 state = 0;
6704 }
6705 if (state == 3) {
6706 if ((ioBytes = fread(&byte, 1, 1, vid_stream->input)) != 1) return 0;
6707 data = ((unsigned int) byte + 0x100);
6708 switch (data) {
6709 case SEQ_START_CODE:
6710 goto got_seq;
6711 case PACK_START_CODE:
6712 case SYSTEM_HEADER_START_CODE:
6713 goto got_sys;
6714 default:
6715 /* keep looking */
6716 state=0;
6717 }
6718 }
6719 }}
6720 }
6721
6722 /* A system layer stream (called after the 1st time), call the specialist */
6723 result = read_sys(vid_stream,0);
6724 return result;
6725}
6726
6727
6728/*
6729 *-------------------------------------------------------------
6730 *
6731 * clear_data_stream
6732 *
6733 * Empties out internal buffers
6734 *
6735 *-------------------------------------------------------------
6736 */
6737void
6738 clear_data_stream(vid_stream)
6740{
6741 /* Only internal buffer is in ReadPacket */
6742 if (vid_stream->sys_layer) {
6743 ReadPacket(KILL_BUFFER, vid_stream);
6744 }
6745}
6746
6747/*
6748 *--------------------------------------------------------------
6749 *
6750 * pure_get_more_data --
6751 * (get_more_data from ver 2.0 with swap added)
6752 *
6753 * Called by get_more_data to read in more data from
6754 * video MPG files (non-system-layer)
6755 *
6756 * Results:
6757 * Input buffer updated, buffer length updated.
6758 * Returns 1 if data read, 0 if EOF, -1 if error.
6759 *
6760 * Side effects:
6761 * None.
6762 *
6763 *--------------------------------------------------------------
6764 */
6765
6766int
6767pure_get_more_data(buf_start, max_length, length_ptr, buf_ptr, vid_stream)
6768 unsigned int *buf_start;
6769 int max_length;
6770 int *length_ptr;
6771 unsigned int **buf_ptr;
6773{
6774
6775 int length, num_read, i;
6776 unsigned int request;
6777 unsigned char *buffer, *mark;
6778 unsigned int *lmark;
6779 int swap=vid_stream->swap;
6780
6781 if (vid_stream->EOF_flag) return 0;
6782
6783 length = *length_ptr;
6784 buffer = (unsigned char *) *buf_ptr;
6785
6786 if (length > 0) {
6787 // a known problem - can overwrite - JAS
6788 //
6789 if (((unsigned char *)buf_start) != buffer) {
6790 memcpy((unsigned char *) buf_start, buffer, (unsigned int) (length*4));
6791 }
6792 mark = ((unsigned char *) (buf_start + length));
6793 }
6794 else {
6795 mark = (unsigned char *) buf_start;
6796 length = 0;
6797 }
6798
6799 request = (max_length-length)*4;
6800
6801
6802 num_read = fread(mark, 1, request, vid_stream->input);
6803
6804 /* Paulo Villegas - 26/1/1993: Correction for 4-byte alignment */
6805 {
6806 int num_read_rounded;
6807 unsigned char *index;
6808
6809 num_read_rounded = 4*(num_read/4);
6810
6811 /* this can happen only if num_read<request; i.e. end of file reached */
6812 if ( num_read_rounded < num_read ) {
6813 num_read_rounded = 4*( num_read/4+1 );
6814
6815 /* fill in with zeros */
6816 for( index=mark+num_read; index<mark+num_read_rounded; *(index++)=0 );
6817
6818 /* advance to the next 4-byte boundary */
6819 num_read = num_read_rounded;
6820 }
6821 }
6822
6823 if (num_read < 0) {
6824 return -1;
6825 } else if (num_read == 0) {
6826 *buf_ptr = buf_start;
6827
6828 /* Make 32 bits after end equal to 0 and 32
6829 * bits after that equal to seq end code
6830 * in order to prevent messy data from infinite
6831 * recursion.
6832 */
6833
6834 *(buf_start + length) = 0x0;
6835 *(buf_start + length+1) = SEQ_END_CODE;
6836
6837 vid_stream->EOF_flag = 1;
6838 return 0;
6839 }
6840
6841 lmark = (unsigned int *) mark;
6842
6843 num_read = num_read/4;
6844
6845 if (swap) {
6846 for (i = 0; i < num_read; i++) {
6847 *lmark = fw_htonl(*lmark);
6848 lmark++;
6849 }
6850 }
6851
6852 *buf_ptr = buf_start;
6853 *length_ptr = length + num_read;
6854
6855 return 1;
6856}
6857
6858/*
6859 *----------------------------------------------------------
6860 *
6861 * read_sys
6862 *
6863 * Parse out a packet of the system layer MPEG file.
6864 *
6865 * Results: Returns 0 if error or EOF
6866 * Returns 1 if more data read (could be just one int)
6867 *
6868 * Side Effects: ReadPacket can change *bs_ptr to be a new buffer
6869 * buf_ptr will remain pointing at *length_ptr (at input)
6870 * into the buffer
6871 * *length_ptr will be changed to the new size
6872 * *max_length can be changed if a new buffer is alloc'd
6873 *
6874 *----------------------------------------------------------
6875 */
6876int read_sys(vid_stream, start)
6878 unsigned int start;
6879 /* start is either a start code or 0 to indicate continued parsing */
6880{
6881 unsigned int startCode;
6882 int errorCode, PacketReply;
6883 unsigned char packetID;
6884 double systemClockTime;
6885 unsigned long muxRate;
6886 /* Statistics */
6887 int match;
6888
6889 if (!start) {
6890 errorCode = ReadStartCode(&startCode,vid_stream);
6891 if (vid_stream->EOF_flag) return 0;
6892 if (errorCode != 0) {
6893 fprintf(stderr, "Unable to read initial pack start code\n");
6894 return 0;
6895 }}
6896 else {
6897 errorCode = 0;
6898 startCode = start;
6899 }
6900
6901 while (1) {
6902 match=FALSE;
6903 if (startCode == PACK_START_CODE) {
6904 match = TRUE;
6905 errorCode = ReadPackHeader( &systemClockTime, &muxRate, vid_stream);
6906 if (errorCode != 0) {
6907 fprintf(stderr, "Error in reading pack header\n");
6908 return 0;
6909 }
6910 errorCode = ReadStartCode( &startCode, vid_stream );
6911 if (errorCode != 0) {
6912 fprintf(stderr, "Error in reading start code\n");
6913 return 0;
6914 }
6915 }
6916 if (startCode == SYSTEM_HEADER_START_CODE) {
6917 match = TRUE;
6918 errorCode = ReadSystemHeader(vid_stream);
6919 if (errorCode != 0) {
6920 fprintf(stderr, "Error in reading system header\n");
6921 return 0;
6922 }
6923 errorCode = ReadStartCode( &startCode, vid_stream );
6924 if (errorCode != 0) {
6925 fprintf(stderr,"Error in reading start code after system header\n");
6926 return 0;
6927 }
6928 }
6929 packetID = startCode & 0xff;
6930 while (((startCode & PACKET_START_CODE_MASK) == PACKET_START_CODE_PREFIX) &&
6931 (packetID >= 0xbc)) {
6932 match = TRUE;
6933 packetID = startCode & 0xff;
6934 PacketReply = ReadPacket(packetID, vid_stream);
6935 switch (PacketReply) {
6936 case 2:
6937 return 1;
6938 case 1:
6939 return 0;
6940 default: /* do nothing */
6941 break;
6942 }
6943 errorCode = ReadStartCode( &startCode, vid_stream );
6944 if (errorCode != 0) {
6945 fprintf(stderr,"Error in start code after packet\n");
6946 return 0;
6947 }
6948 if (startCode == PACK_START_CODE || startCode == ISO_11172_END_CODE) {
6949 break;
6950 }
6951 }
6952
6953 if (startCode == ISO_11172_END_CODE) {
6954 match = TRUE;
6955 if (vid_stream->Parse_done) {
6956 return 1;
6957 }
6958 ReadPacket(NOT_PACKET_ID, vid_stream);
6959 vid_stream->Parse_done = TRUE;
6960 return 1;
6961 }
6962 if (errorCode != 0)
6963 return 1;
6964 if (! match) {
6965 fprintf(stderr,"\nNo match found for start code %08x in system layer, skipping\n",startCode);
6966 startCode = find_start_code(vid_stream->input);
6967 if (startCode == EOF) {
6968 vid_stream->EOF_flag = 1;
6969 return 0;
6970 }
6971 }
6972 }
6973}
6974
6975
6976/*
6977 *-----------------------------------------------------------
6978 *
6979 * ReadStartCode
6980 *
6981 * Parses a start code out of the stream
6982 *
6983 * Results/Side Effects: Sets *startCode to the code, returns
6984 * 1 on error, 0 on success
6985 *
6986 *-----------------------------------------------------------
6987 */
6988int ReadStartCode(startCode, vid_stream)
6989 unsigned int *startCode;
6991{
6992 int numRead;
6993
6994 numRead = fread((unsigned char *)startCode, 1, 4, vid_stream->input);
6995 *startCode = fw_htonl(*startCode);
6996
6997 if (numRead < 4) {
6998 vid_stream->EOF_flag = 1;
6999 return 1;
7000 }
7001
7002 if ((*startCode&0xfffffe00) != 0) {
7003 fprintf(stderr,"Problem with system layer parse, skipping to start code\n");
7004 *startCode = find_start_code(vid_stream->input);
7005 if (*startCode == EOF) {
7006 vid_stream->EOF_flag = TRUE;
7007 return 0;
7008 }
7009 }
7010
7011 return 0;
7012}
7013
7014
7015/*
7016 *-----------------------------------------------------------
7017 *
7018 * find_start_code
7019 *
7020 * Parses a start code out of the stream by tossing bytes until it gets one
7021 *
7022 * Results/Side Effects: Parses bytes of the stream, returns code
7023 * Returns EOF in case of end of file
7024 *
7025 *-----------------------------------------------------------
7026 */
7027int find_start_code(input)
7028 FILE *input;
7029{
7030 NO_ZEROS:
7031 switch(fgetc(input)) {
7032 case 0: goto ONE_ZERO;
7033 case EOF: goto EOF_FOUND;
7034 default: goto NO_ZEROS;
7035 }
7036
7037 ONE_ZERO:
7038 switch(fgetc(input)) {
7039 case 0: goto TWO_ZEROS;
7040 case EOF: goto EOF_FOUND;
7041 default: goto NO_ZEROS;
7042 }
7043
7044 TWO_ZEROS:
7045 switch(fgetc(input)) {
7046 case 0x01: goto CODE_FOUND;
7047 case 0x00: goto TWO_ZEROS;
7048 case EOF: goto EOF_FOUND;
7049 default: goto NO_ZEROS;
7050 }
7051
7052 CODE_FOUND:
7053 return 0x00000100+fgetc(input);
7054
7055 EOF_FOUND: /* received EOF */
7056 return EOF;
7057}
7058
7059
7060
7061
7062/*
7063 *-----------------------------------------------------------------
7064 *
7065 * ReadPackHeader
7066 *
7067 * Parses out the PACK header
7068 *
7069 * Returns: 1 on error, 0 on success
7070 *
7071 *-------------------------------------------------------------------
7072 */
7073int ReadPackHeader(systemClockTime,muxRate, vid_stream)
7074 double *systemClockTime;
7075 unsigned long *muxRate;
7077{
7078 int numRead;
7079 unsigned char inputBuffer[PACK_HEADER_SIZE];
7080 unsigned long systemClockRef;
7081 unsigned char systemClockRefHiBit;
7082 int errorCode;
7083
7084 numRead = fread(inputBuffer, 1, PACK_HEADER_SIZE, vid_stream->input);
7085 if (numRead < PACK_HEADER_SIZE) {
7086 vid_stream->EOF_flag = 1;
7087 return 1;
7088 }
7089 ReadTimeStamp(inputBuffer, &systemClockRefHiBit, &systemClockRef);
7090 errorCode = MakeFloatClockTime(systemClockRefHiBit, systemClockRef,
7091 systemClockTime);
7092 ReadRate(&inputBuffer[5], muxRate);
7093 *muxRate *= MUX_RATE_SCALE_FACTOR;
7094 return 0;
7095}
7096
7097
7098/*
7099 *------------------------------------------------------------------
7100 *
7101 * ReadSystemHeader
7102 *
7103 * Parse out the system header, setup out stream IDs for parsing packets
7104 *
7105 * Results: Returns 1 on error, 0 on success.
7106 * Sets gAudioStreamID and gVideoStreamID
7107 *
7108 *------------------------------------------------------------------
7109 */
7110int ReadSystemHeader(vid_stream)
7112{
7113 unsigned char *inputBuffer = NULL;
7114 int numRead;
7115 int pos;
7116 unsigned short headerSize;
7117 unsigned char streamID;
7118
7119 numRead = fread((char *)&headerSize, 1, 2, vid_stream->input);
7120 headerSize = fw_ntohs(headerSize);
7121 if (numRead != 2) {
7122 vid_stream->EOF_flag = 1;
7123 return 1;
7124 }
7125 inputBuffer = (unsigned char *) malloc((unsigned int) headerSize+1);
7126 if (inputBuffer == NULL) {
7127 return 1;
7128 }
7129 inputBuffer[headerSize]=0;
7130 numRead = fread(inputBuffer, 1, headerSize, vid_stream->input);
7131 /* Brown - get rid of Ansi C complaints */
7132 if (numRead < (int) headerSize) {
7133 vid_stream->EOF_flag = 1;
7134 return 1;
7135 }
7136
7137 pos = 6;
7138 while ((inputBuffer[pos] & 0x80) == 0x80) {
7139 streamID = inputBuffer[pos];
7140 switch (streamID) {
7141 case STD_VIDEO_STREAM_ID:
7142 break;
7143 case STD_AUDIO_STREAM_ID:
7144 break;
7145 case RESERVED_STREAM_ID:
7146 break;
7147 case PADDING_STREAM_ID:
7148 break;
7149 case PRIVATE_STREAM_1_ID:
7150 break;
7151 case PRIVATE_STREAM_2_ID:
7152 break;
7153 default:
7154 if (streamID < MIN_STREAM_ID_ID) {
7155 return 1;
7156 }
7157 switch (streamID >> 4) {
7158 case 0xc:
7159 case 0xd:
7160 vid_stream->gAudioStreamID = streamID;
7161 break;
7162 case 0xe:
7163 if ((vid_stream->gVideoStreamID != 0) &&
7164 (vid_stream->gVideoStreamID!=streamID)) {
7165 break;
7166 }
7167 vid_stream->gVideoStreamID = streamID;
7168 break;
7169 case 0xf:
7170/*Brown - deglobalized gReservedStreamID */
7171 vid_stream->gReservedStreamID = streamID;
7172 break;
7173 }
7174 break;
7175 }
7176 pos += 3;
7177 }
7178 if (inputBuffer != NULL)
7179 free(inputBuffer);
7180 return 0;
7181}
7182
7183
7184/*
7185 *-----------------------------------------------------------------
7186 *
7187 * ReadPacket
7188 *
7189 * Reads a single packet out of the stream, and puts it in the
7190 * buffer if it is video.
7191 *
7192 * Results:
7193 * Changes the value of *length_ptr to be the new length (plus old)
7194 * If the buffer is too small, can change *bs_ptr, *max_length, and
7195 * buf_ptr to be correct for a newly allocated buffer.
7196 *
7197 * State:
7198 * The buffer is in ints, but the packets can be an arbitrary number
7199 * of bytes, so leftover bytes are kept in the mpeg_VidStream variable and
7200 * are added on the next call.
7201 *
7202 *-----------------------------------------------------------------
7203 */
7204#ifdef __STDC__
7205int ReadPacket(unsigned char packetID, mpeg_VidStream *vid_stream)
7206#else
7207int ReadPacket(packetID, vid_stream)
7208 unsigned char packetID;
7210#endif
7211
7212 /* Returns:
7213 0 - no error, but not video packet we want
7214 1 - error
7215 2 - got video packet into buffer
7216 */
7217{
7218 unsigned int **bs_ptr=&vid_stream->buf_start;
7219 int *max_length = &vid_stream->max_buf_length;
7220 int *length_ptr=&vid_stream->buf_length;
7221 unsigned int **buf_ptr=&vid_stream->buffer;
7222 int ioBytes;
7223 unsigned char nextByte;
7224 unsigned short packetLength;
7225 unsigned char *packetBuffer = NULL;
7226 int pos;
7227 int numStuffBytes = 0;
7228 unsigned int packetDataLength;
7229 int byte_length;
7230 unsigned char scratch[10];
7231 /* Leftovers from previous video packets */
7232
7233 if (packetID == NOT_PACKET_ID) {
7234 /* Gross hack to handle unread bytes before end of stream */
7235 if (vid_stream->num_left != 0) {
7236 /* Sigh, deal with previous leftovers */
7237 *(*buf_ptr+*length_ptr) = vid_stream->leftover_bytes;
7238 *(*buf_ptr+*length_ptr+1) = ISO_11172_END_CODE;
7239 *length_ptr += 2;
7240 } else {
7241 *(*buf_ptr+*length_ptr) = ISO_11172_END_CODE;
7242 *length_ptr += 1;
7243 }
7244 return 1;
7245 } else if (packetID==KILL_BUFFER) {
7246 vid_stream->num_left=0;
7247 vid_stream->leftover_bytes=0;
7248 return 0;
7249 }
7250
7251 ioBytes = fread(&packetLength, 1, 2, vid_stream->input);
7252 packetLength = fw_htons(packetLength); //Q. should this be ntohs?
7253 if (ioBytes < 2) {
7254 return 1;
7255 }
7256 if (packetID == vid_stream->gAudioStreamID) {
7257 }
7258 else if (packetID == vid_stream->gVideoStreamID) {
7259 }
7260 else {
7261 switch (packetID) {
7262 case PADDING_STREAM_ID:
7263 case RESERVED_STREAM_ID:
7264 case PRIVATE_STREAM_1_ID:
7265 case PRIVATE_STREAM_2_ID:
7266 break;
7267 default:
7268 fprintf(stderr, "\nUnknown packet type encountered. P'bly audio? (%x) at %d\n",
7269 packetID,(int) ftell(vid_stream->input));
7270 }
7271 if (packetID != vid_stream->gVideoStreamID) {/* changed by jim */
7272 fseek(vid_stream->input, packetLength, 1);
7273 return 0;
7274 }
7275 }
7276
7277 fread(&nextByte,1,1,vid_stream->input);
7278 pos = 0;
7279 while (nextByte & 0x80) {
7280 ++numStuffBytes;
7281 ++pos;
7282 fread(&nextByte,1,1,vid_stream->input);
7283 }
7284 if ((nextByte >> 6) == 0x01) {
7285 pos += 2;
7286 fread(&nextByte,1,1,vid_stream->input);
7287 fread(&nextByte,1,1,vid_stream->input);
7288 }
7289 if ((nextByte >> 4) == 0x02) {
7290 scratch[0] = nextByte; /* jim */
7291 fread(&scratch[1],1,4,vid_stream->input); /* jim */
7292 fread(&nextByte,1,1,vid_stream->input);
7293 pos += 5;
7294 }
7295 else if ((nextByte >> 4) == 0x03) {
7296 scratch[0] = nextByte; /* jim */
7297 fread(&scratch[1],1,9,vid_stream->input); /* jim */
7298 fread(&nextByte,1,1,vid_stream->input);
7299 pos += 10;
7300 }
7301 else {
7302 fread(&nextByte,1,1,vid_stream->input);
7303 pos += 1;
7304 }
7305 /* Read all the headers, now make room for packet */
7306printf ("buf_ptr %p\n",*buf_ptr);
7307printf ("length_ptr %p\n",length_ptr);
7308 if (*bs_ptr + *max_length < *buf_ptr+ packetLength/4 + *length_ptr) {
7309 /* Brown - get rid of Ansi C complaints */
7310 if (*max_length - *length_ptr < (int) packetLength/4) {
7311 /* Buffer too small for a packet (plus whats there),
7312 * time to enlarge it!
7313 */
7314 unsigned int *old = *bs_ptr;
7315 *max_length = *length_ptr + packetLength/2;
7316 *bs_ptr=(unsigned int *)malloc(*max_length*4);
7317 if (*bs_ptr == NULL) {
7318 return 1;
7319 }
7320 memcpy((unsigned char *)*bs_ptr, *buf_ptr, (unsigned int) *length_ptr*4);
7321 free(old);
7322 *buf_ptr = *bs_ptr;
7323 } else {
7324 memcpy((unsigned char *)*bs_ptr, *buf_ptr, (unsigned int) *length_ptr*4);
7325 *buf_ptr = *bs_ptr;
7326 }}
7327 byte_length = *length_ptr*4;
7328 if (vid_stream->num_left != 0) {
7329 /* Sigh, deal with previous leftovers */
7330 byte_length += vid_stream->num_left;
7331 *(*buf_ptr+*length_ptr) = vid_stream->leftover_bytes;
7332 }
7333 packetBuffer=((unsigned char *)*buf_ptr)+byte_length;
7334 packetDataLength = packetLength - pos;
7335 *packetBuffer++ = nextByte;
7336/* Brown - deglobalize gVideoStreamID */
7337 if (packetID == vid_stream->gVideoStreamID) {
7338 ioBytes = fread(packetBuffer, 1, packetDataLength-1, vid_stream->input);
7339 if (ioBytes != packetDataLength-1) {
7340 vid_stream->EOF_flag = 1;
7341 return 1;
7342 }
7343 if (1 != fw_ntohl(1)) {
7344 unsigned int *mark = *buf_ptr+*length_ptr;
7345 unsigned int i;
7346
7347 for (i=0; i < ((packetDataLength+
7348 vid_stream->num_left)&0xfffffffc); i+=4) {
7349 *mark=fw_ntohl(*mark);
7350 mark++;
7351 }
7352 }
7353 byte_length = byte_length + packetDataLength;
7354 vid_stream->num_left = byte_length % 4;
7355 *length_ptr = byte_length / 4;
7356 vid_stream->leftover_bytes = *(*buf_ptr + *length_ptr);
7357 return 2;
7358 }
7359 else if (packetID == vid_stream->gAudioStreamID) {
7360 packetBuffer = (unsigned char *)(*buf_ptr + *length_ptr + 1);
7361 fread(packetBuffer, 1, packetDataLength - 1, vid_stream->input);
7362 }
7363 else /* Donno what it is, just nuke it */ {
7364 /* This code should be unreachable */
7365 packetBuffer = (unsigned char *)(*buf_ptr + *length_ptr + 1);
7366 fread(packetBuffer, 1, packetDataLength - 1, vid_stream->input);
7367 }
7368 return 0;
7369}
7370
7371
7372/*
7373 * The remaining procedures are formatting utility procedures.
7374 */
7375void ReadTimeStamp(inputBuffer,hiBit,low4Bytes)
7376 unsigned char *inputBuffer, *hiBit;
7377 unsigned long *low4Bytes;
7378{
7379 *hiBit = ((unsigned long)inputBuffer[0] >> 3) & 0x01;
7380 *low4Bytes = (((unsigned long)inputBuffer[0] >> 1) & 0x03) << 30;
7381 *low4Bytes |= (unsigned long)inputBuffer[1] << 22;
7382 *low4Bytes |= ((unsigned long)inputBuffer[2] >> 1) << 15;
7383 *low4Bytes |= (unsigned long)inputBuffer[3] << 7;
7384 *low4Bytes |= ((unsigned long)inputBuffer[4]) >> 1;
7385}
7386
7387void ReadSTD(inputBuffer,stdBufferScale,stdBufferSize)
7388unsigned char *inputBuffer;
7389unsigned char *stdBufferScale;
7390unsigned long *stdBufferSize;
7391{
7392 /* Brown - get rid of ANSI C complaints */
7393 *stdBufferScale = ((int)(inputBuffer[0] & 0x20) >> 5);
7394 *stdBufferSize = ((unsigned long)inputBuffer[0] & 0x1f) << 8;
7395 *stdBufferSize |= (unsigned long)inputBuffer[1];
7396}
7397
7398
7399void ReadRate(inputBuffer,rate)
7400 unsigned char *inputBuffer;
7401 unsigned long *rate;
7402{
7403 *rate = (inputBuffer[0] & 0x7f) << 15;
7404 *rate |= inputBuffer[1] << 7;
7405 /* Brown - get rid of ANSI C complaints */
7406 *rate |= (int) (inputBuffer[2] & 0xfe) >> 1;
7407}
7408
7409#define FLOAT_0x10000 (double)((unsigned long)1 << 16)
7410
7411#ifdef __STDC__
7412int MakeFloatClockTime(unsigned char hiBit, unsigned long low4Bytes,
7413 double * floatClockTime)
7414#else
7415int MakeFloatClockTime(hiBit,low4Bytes,floatClockTime)
7416 unsigned char hiBit;
7417 unsigned long low4Bytes;
7418 double *floatClockTime;
7419#endif
7420{
7421 if (hiBit != 0 && hiBit != 1) {
7422 *floatClockTime = 0.0;
7423 return 1;
7424 }
7425 *floatClockTime
7426 = (double)hiBit*FLOAT_0x10000*FLOAT_0x10000 + (double)low4Bytes;
7427 *floatClockTime /= (double)STD_SYSTEM_CLOCK_FREQ;
7428 return 0;
7429}
7430
7431
7432
7433/* Define buffer length. */
7434#define BUF_LENGTH 80000
7435
7436# define P(s) ()
7437void int_handler P((void ));
7438void bad_handler P((void ));
7439
7440/* Global file pointer to incoming data. */
7441FILE *mpegfile;
7442
7443/*
7444 *--------------------------------------------------------------
7445 *
7446 * main --
7447 *
7448 * Parses command line, starts decoding and displaying.
7449 *
7450 * Results:
7451 * None.
7452 *
7453 * Side effects:
7454 * None.
7455 *
7456 *--------------------------------------------------------------
7457 */
7458
7459
7460void mpg_main(char *fname, int *x,int *y,int *depth,int *fc,char **ptr) {
7461
7462 mpeg_VidStream *theStream;
7463 int ppm_width = -1, ppm_height = -1, ppm_modulus = -1;
7464
7465 /* save pointers */
7466 frameCount = fc;
7467 *frameCount = 1;
7468 ySize = y;
7469 xSize = x;
7470 *depth = 3; /* always this depth... */
7471 dataPointer = NULL;
7472
7473 theStream = NULL;
7474
7475 fflush(stdout);
7476 mpegfile=fopen(fname, "r");
7477
7478 if (mpegfile == NULL) {
7479 printf("Could not open MovieTexture file %s\n", fname);
7480 *frameCount = 0;
7481 return;
7482 }
7483
7484 init_tables();
7485
7486 InitColorDither(1);
7487
7488 InitCrop();
7489 theStream = mpg_NewVidStream((unsigned int) BUF_LENGTH);
7490 theStream->ppm_width = ppm_width;
7491 theStream->ppm_height = ppm_height;
7492 theStream->ppm_modulus = ppm_modulus;
7493 theStream->input = mpegfile;
7494 theStream->filename = fname;
7495 theStream->matched_depth = 24;
7496
7497 if (mpegVidRsrc(0, theStream, 1)==NULL) {
7498 /* stream has already been destroyed */
7499 printf("Skipping movie \"%s\" - not an MPEG stream\n",
7500 fname);
7501 if (theStream!=NULL) {
7502 printf ("theStream != NULL, destroying, part1\n");
7503 Destroympeg_VidStream(theStream);
7504 }
7505 }
7506
7507 /* Start time for each movie - do after windows are mapped */
7508 if (theStream != NULL) theStream->realTimeStart = ReadSysClock();
7509 if (theStream != NULL) {
7510 while (theStream->film_has_ended == FALSE) {
7511 mpegVidRsrc(0, theStream, 0);
7512 }
7513 }
7514 if (L_tab!=NULL)
7515 free((int *)L_tab);
7516 if (Cr_r_tab!=NULL)
7517 free ((int *)Cr_r_tab);
7518 if (Cr_g_tab!=NULL)
7519 free ((int *)Cr_g_tab);
7520 if (Cb_g_tab!=NULL)
7521 free ((int *)Cb_g_tab);
7522 if (Cb_b_tab!=NULL)
7523 free ((int *)Cb_b_tab);
7524
7525 if (r_2_pix_alloc!=NULL)
7526 free ((long *) r_2_pix_alloc);
7527 if (g_2_pix_alloc!=NULL)
7528 free ((long *) g_2_pix_alloc);
7529 if (b_2_pix_alloc!=NULL)
7530 free ((long *) b_2_pix_alloc);
7531
7532 /* zero these in case we have another mpg file to do */
7533 L_tab = NULL; Cr_r_tab = NULL; Cr_g_tab = NULL; Cb_g_tab = NULL;Cb_b_tab = NULL;
7534 r_2_pix_alloc = NULL; g_2_pix_alloc= NULL;b_2_pix_alloc = NULL;
7535
7536 fclose(mpegfile);
7537 *ptr = dataPointer;
7538 *fc = (*frameCount)--;
7539}