1 #ifndef _BITSTREAM_H_
2 #define _BITSTREAM_H_
3 
4 #ifdef HAVE_CONFIG_H
5 # include "config.h"
6 #endif
7 
8 #include <stdint.h>
9 
10 #define BSWAP(x) x=((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >>  8) | (((x) & 0x0000ff00) <<  8) | (((x) & 0x000000ff) << 24))
11 
12 #define EDGE_SIZE  32
13 
14 typedef struct
15 {
16 	uint8_t * y;
17 	uint8_t * u;
18 	uint8_t * v;
19 } IMAGE;
20 
21 
22 /* Inter-coded macroblock, 1 motion vector */
23 #define MODE_INTER		0
24 /* Inter-coded macroblock + dquant */
25 #define MODE_INTER_Q	1
26 /* Inter-coded macroblock, 4 motion vectors */
27 #define MODE_INTER4V	2
28 /* Intra-coded macroblock */
29 #define	MODE_INTRA		3
30 /* Intra-coded macroblock + dquant */
31 #define MODE_INTRA_Q	4
32 
33 
34 typedef struct
35 {
36 	int32_t x;
37 	int32_t y;
38 } VECTOR;
39 
40 
41 #define MBPRED_SIZE  15
42 
43 
44 typedef struct
45 {
46 	VECTOR mvs[4];
47 
48     int16_t pred_values[6][MBPRED_SIZE];
49     uint8_t acpred_directions[6];
50 
51 	uint32_t mode;
52 	uint32_t quant;		// absolute quant
53 
54 	// uint32_t cbp;
55 } MACROBLOCK;
56 
57 
58 
59 typedef struct
60 {
61 	// bitstream
62 
63 	uint32_t time_inc_bits;
64 	uint32_t quant_bits;
65 	uint32_t quant_type;
66 
67 	// image
68 
69 	uint32_t width;
70 	uint32_t height;
71 	uint32_t edged_width;
72 	uint32_t edged_height;
73 
74 	IMAGE cur;
75 	IMAGE refn;
76 	IMAGE refh;
77 	IMAGE refv;
78 	IMAGE refhv;
79 
80 	// macroblock
81 
82 	uint32_t mb_width;
83 	uint32_t mb_height;
84 	MACROBLOCK * mbs;
85 
86 
87 } DECODER;
88 
89 // vop coding types
90 // intra, prediction, backward, sprite, not_coded
91 #define I_VOP	0
92 #define P_VOP	1
93 #define B_VOP	2
94 #define S_VOP	3
95 #define N_VOP	4
96 
97 typedef struct
98 {
99 	uint32_t bufa;
100 	uint32_t bufb;
101 	uint32_t pos;
102 	uint32_t * head;
103 } BITSTREAM;
104 
105 
106 
107 // header stuff
108 void bs_init_tc(BITSTREAM * bs, char *bitstream);
109 int bs_vol(BITSTREAM * bs, DECODER * dec);
110 int bs_vop(BITSTREAM * bs, DECODER * dec, uint32_t * rounding, uint32_t * quant, uint32_t * fcode);
111 
112 
bs_show(BITSTREAM * const bs,const uint32_t bits)113 static uint32_t __inline bs_show(BITSTREAM * const bs, const uint32_t bits)
114 {
115 	int nbit = (bits + bs->pos) - 32;
116 	if (nbit > 0)
117 	{
118 		return ((bs->bufa & (0xffffffff >> bs->pos)) << nbit) |
119 				(bs->bufb >> (32 - nbit));
120 	}
121 	else
122 	{
123 		return (bs->bufa & (0xffffffff >> bs->pos)) >> (32 - bs->pos - bits);
124 	}
125 }
126 
127 
128 
bs_skip(BITSTREAM * const bs,const uint32_t bits)129 static __inline void bs_skip(BITSTREAM * const bs, const uint32_t bits)
130 {
131 	bs->pos += bits;
132 
133 	if (bs->pos >= 32)
134 	{
135 		uint32_t tmp;
136 
137 		bs->bufa = bs->bufb;
138 		tmp = *(uint32_t *)bs->head;
139 #ifndef BIG_ENDIAN
140 		BSWAP(tmp);
141 #endif
142 		bs->bufb = tmp;
143 		bs->head ++;
144 		bs->pos -= 32;
145 	}
146 }
147 
148 
149 
bs_bytealign(BITSTREAM * const bs)150 static __inline void bs_bytealign(BITSTREAM * const bs)
151 {
152 	uint32_t remainder = bs->pos % 8;
153 	if (remainder)
154 	{
155 		bs_skip(bs, 8 - remainder);
156 	}
157 }
158 
159 
160 
bs_get(BITSTREAM * const bs,const uint32_t n)161 static uint32_t __inline bs_get(BITSTREAM * const bs, const uint32_t n)
162 {
163 	uint32_t ret = bs_show(bs, n);
164 	bs_skip(bs, n);
165 	return ret;
166 }
167 
168 
bs_get1(BITSTREAM * const bs)169 static uint32_t __inline bs_get1(BITSTREAM * const bs)
170 {
171 	return bs_get(bs, 1);
172 }
173 
174 
175 #endif /* _BITSTREAM_H_ */
176