1 /*
2  * mpeg2_internal.h
3  * Copyright (C) 2000-2003 Michel Lespinasse <walken@zoy.org>
4  * Copyright (C) 1999-2000 Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
5  *
6  * This file is part of mpeg2dec, a free MPEG-2 video stream decoder.
7  * See http://libmpeg2.sourceforge.net/ for updates.
8  *
9  * mpeg2dec is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * mpeg2dec is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  */
23 
24 #ifndef LIBMPEG2_MPEG2_INTERNAL_H
25 #define LIBMPEG2_MPEG2_INTERNAL_H
26 
27 #define STATE_INTERNAL_NORETURN ((mpeg2_state_t)-1)
28 
29 /* macroblock modes */
30 #define MACROBLOCK_INTRA 1
31 #define MACROBLOCK_PATTERN 2
32 #define MACROBLOCK_MOTION_BACKWARD 4
33 #define MACROBLOCK_MOTION_FORWARD 8
34 #define MACROBLOCK_QUANT 16
35 #define DCT_TYPE_INTERLACED 32
36 /* motion_type */
37 #define MOTION_TYPE_SHIFT 6
38 #define MC_FIELD 1
39 #define MC_FRAME 2
40 #define MC_16X8 2
41 #define MC_DMV 3
42 
43 /* picture structure */
44 #define TOP_FIELD 1
45 #define BOTTOM_FIELD 2
46 #define FRAME_PICTURE 3
47 
48 /* picture coding type */
49 #define I_TYPE 1
50 #define P_TYPE 2
51 #define B_TYPE 3
52 #define D_TYPE 4
53 
54 typedef void mpeg2_mc_fct (uint8_t *, const uint8_t *, int, int);
55 
56 typedef struct {
57     uint8_t * ref[2][3];
58     uint8_t ** ref2[2];
59     int pmv[2][2];
60     int f_code[2];
61 } motion_t;
62 
63 typedef void motion_parser_t (mpeg2_decoder_t * decoder,
64 			      motion_t * motion,
65 			      mpeg2_mc_fct * const * table);
66 
67 struct mpeg2_decoder_s {
68     /* first, state that carries information from one macroblock to the */
69     /* next inside a slice, and is never used outside of mpeg2_slice() */
70 
71     /* bit parsing stuff */
72     uint32_t bitstream_buf;		/* current 32 bit working set */
73     int bitstream_bits;			/* used bits in working set */
74     const uint8_t * bitstream_ptr;	/* buffer with stream data */
75 
76     uint8_t * dest[3];
77 
78     int offset;
79     int stride;
80     int uv_stride;
81     int slice_stride;
82     int slice_uv_stride;
83     int stride_frame;
84     unsigned int limit_x;
85     unsigned int limit_y_16;
86     unsigned int limit_y_8;
87     unsigned int limit_y;
88 
89     /* Motion vectors */
90     /* The f_ and b_ correspond to the forward and backward motion */
91     /* predictors */
92     motion_t b_motion;
93     motion_t f_motion;
94     motion_parser_t * motion_parser[5];
95 
96     /* predictor for DC coefficients in intra blocks */
97     int16_t dc_dct_pred[3];
98 
99     /* DCT coefficients */
100     int16_t DCTblock[64] ATTR_ALIGN(64);
101 
102     uint8_t * picture_dest[3];
103     void (* convert) (void * convert_id, uint8_t * const * src,
104 		      unsigned int v_offset);
105     void * convert_id;
106 
107     int dmv_offset;
108     unsigned int v_offset;
109 
110     /* now non-slice-specific information */
111 
112     /* sequence header stuff */
113     uint16_t * quantizer_matrix[4];
114     uint16_t (* chroma_quantizer[2])[64];
115     uint16_t quantizer_prescale[4][32][64];
116 
117     /* The width and height of the picture snapped to macroblock units */
118     int width;
119     int height;
120     int vertical_position_extension;
121     int chroma_format;
122 
123     /* picture header stuff */
124 
125     /* what type of picture this is (I, P, B, D) */
126     int coding_type;
127 
128     /* picture coding extension stuff */
129 
130     /* quantization factor for intra dc coefficients */
131     int intra_dc_precision;
132     /* top/bottom/both fields */
133     int picture_structure;
134     /* bool to indicate all predictions are frame based */
135     int frame_pred_frame_dct;
136     /* bool to indicate whether intra blocks have motion vectors */
137     /* (for concealment) */
138     int concealment_motion_vectors;
139     /* bool to use different vlc tables */
140     int intra_vlc_format;
141     /* used for DMV MC */
142     int top_field_first;
143 
144     /* stuff derived from bitstream */
145 
146     /* pointer to the zigzag scan we're supposed to be using */
147     const uint8_t * scan;
148 
149     int second_field;
150 
151     int mpeg1;
152 
153     /* XXX: stuff due to xine shit */
154     int8_t q_scale_type;
155 };
156 
157 typedef struct {
158     mpeg2_fbuf_t fbuf;
159 } fbuf_alloc_t;
160 
161 struct mpeg2dec_s {
162     mpeg2_decoder_t decoder;
163 
164     mpeg2_info_t info;
165 
166     uint32_t shift;
167     int is_display_initialized;
168     mpeg2_state_t (* action) (struct mpeg2dec_s * mpeg2dec);
169     mpeg2_state_t state;
170     uint32_t ext_state;
171 
172     /* allocated in init - gcc has problems allocating such big structures */
173     uint8_t * chunk_buffer;
174     /* pointer to start of the current chunk */
175     uint8_t * chunk_start;
176     /* pointer to current position in chunk_buffer */
177     uint8_t * chunk_ptr;
178     /* last start code ? */
179     uint8_t code;
180 
181     /* picture tags */
182     uint32_t tag_current, tag2_current, tag_previous, tag2_previous;
183     int num_tags;
184     int bytes_since_tag;
185 
186     int first;
187     int alloc_index_user;
188     int alloc_index;
189     uint8_t first_decode_slice;
190     uint8_t nb_decode_slices;
191 
192     unsigned int user_data_len;
193 
194     mpeg2_sequence_t new_sequence;
195     mpeg2_sequence_t sequence;
196     mpeg2_gop_t new_gop;
197     mpeg2_gop_t gop;
198     mpeg2_picture_t new_picture;
199     mpeg2_picture_t pictures[4];
200     mpeg2_picture_t * picture;
201     /*const*/ mpeg2_fbuf_t * fbuf[3];	/* 0: current fbuf, 1-2: prediction fbufs */
202 
203     fbuf_alloc_t fbuf_alloc[3];
204     int custom_fbuf;
205 
206     uint8_t * yuv_buf[3][3];
207     int yuv_index;
208     mpeg2_convert_t * convert;
209     void * convert_arg;
210     unsigned int convert_id_size;
211     int convert_stride;
212     void (* convert_start) (void * id, const mpeg2_fbuf_t * fbuf,
213 			    const mpeg2_picture_t * picture,
214 			    const mpeg2_gop_t * gop);
215 
216     uint8_t * buf_start;
217     uint8_t * buf_end;
218 
219     int16_t display_offset_x, display_offset_y;
220 
221     int copy_matrix;
222     int8_t scaled[4]; /* XXX: MOVED */
223     //int8_t q_scale_type, scaled[4];
224     uint8_t quantizer_matrix[4][64];
225     uint8_t new_quantizer_matrix[4][64];
226 };
227 
228 typedef struct {
229 #ifdef ARCH_PPC
230     uint8_t regv[12*16];
231 #endif
232     int dummy;
233 } cpu_state_t;
234 
235 /* cpu_accel.c */
236 uint32_t mpeg2_detect_accel (uint32_t accel);
237 
238 /* cpu_state.c */
239 void mpeg2_cpu_state_init (uint32_t accel);
240 
241 /* decode.c */
242 mpeg2_state_t mpeg2_seek_header (mpeg2dec_t * mpeg2dec);
243 mpeg2_state_t mpeg2_parse_header (mpeg2dec_t * mpeg2dec);
244 
245 /* header.c */
246 void mpeg2_header_state_init (mpeg2dec_t * mpeg2dec);
247 void mpeg2_reset_info (mpeg2_info_t * info);
248 int mpeg2_header_sequence (mpeg2dec_t * mpeg2dec);
249 int mpeg2_header_gop (mpeg2dec_t * mpeg2dec);
250 mpeg2_state_t mpeg2_header_picture_start (mpeg2dec_t * mpeg2dec);
251 int mpeg2_header_picture (mpeg2dec_t * mpeg2dec);
252 int mpeg2_header_extension (mpeg2dec_t * mpeg2dec);
253 int mpeg2_header_user_data (mpeg2dec_t * mpeg2dec);
254 void mpeg2_header_sequence_finalize (mpeg2dec_t * mpeg2dec);
255 void mpeg2_header_gop_finalize (mpeg2dec_t * mpeg2dec);
256 void mpeg2_header_picture_finalize (mpeg2dec_t * mpeg2dec, uint32_t accels);
257 mpeg2_state_t mpeg2_header_slice_start (mpeg2dec_t * mpeg2dec);
258 mpeg2_state_t mpeg2_header_end (mpeg2dec_t * mpeg2dec);
259 void mpeg2_set_fbuf (mpeg2dec_t * mpeg2dec, int b_type);
260 
261 /* idct.c */
262 extern void mpeg2_idct_init (uint32_t accel);
263 extern uint8_t mpeg2_scan_norm[64];
264 extern uint8_t mpeg2_scan_alt[64];
265 
266 /* idct_mmx.c */
267 void mpeg2_idct_copy_sse2 (int16_t * block, uint8_t * dest, int stride);
268 void mpeg2_idct_add_sse2 (int last, int16_t * block,
269 			  uint8_t * dest, int stride);
270 void mpeg2_idct_copy_mmxext (int16_t * block, uint8_t * dest, int stride);
271 void mpeg2_idct_add_mmxext (int last, int16_t * block,
272 			    uint8_t * dest, int stride);
273 void mpeg2_idct_copy_mmx (int16_t * block, uint8_t * dest, int stride);
274 void mpeg2_idct_add_mmx (int last, int16_t * block,
275 			 uint8_t * dest, int stride);
276 void mpeg2_idct_mmx_init (void);
277 
278 /* idct_altivec.c */
279 void mpeg2_idct_copy_altivec (int16_t * block, uint8_t * dest, int stride);
280 void mpeg2_idct_add_altivec (int last, int16_t * block,
281 			     uint8_t * dest, int stride);
282 void mpeg2_idct_altivec_init (void);
283 
284 /* idct_alpha.c */
285 void mpeg2_idct_copy_mvi (int16_t * block, uint8_t * dest, int stride);
286 void mpeg2_idct_add_mvi (int last, int16_t * block,
287 			 uint8_t * dest, int stride);
288 void mpeg2_idct_copy_alpha (int16_t * block, uint8_t * dest, int stride);
289 void mpeg2_idct_add_alpha (int last, int16_t * block,
290 			   uint8_t * dest, int stride);
291 void mpeg2_idct_alpha_init (void);
292 
293 /* motion_comp.c */
294 void mpeg2_mc_init (uint32_t accel);
295 
296 typedef struct {
297     mpeg2_mc_fct * put [8];
298     mpeg2_mc_fct * avg [8];
299 } mpeg2_mc_t;
300 
301 #define MPEG2_MC_EXTERN(x) mpeg2_mc_t mpeg2_mc_##x = {			  \
302     {MC_put_o_16_##x, MC_put_x_16_##x, MC_put_y_16_##x, MC_put_xy_16_##x, \
303      MC_put_o_8_##x,  MC_put_x_8_##x,  MC_put_y_8_##x,  MC_put_xy_8_##x}, \
304     {MC_avg_o_16_##x, MC_avg_x_16_##x, MC_avg_y_16_##x, MC_avg_xy_16_##x, \
305      MC_avg_o_8_##x,  MC_avg_x_8_##x,  MC_avg_y_8_##x,  MC_avg_xy_8_##x}  \
306 };
307 
308 extern mpeg2_mc_t mpeg2_mc_c;
309 extern mpeg2_mc_t mpeg2_mc_mmx;
310 extern mpeg2_mc_t mpeg2_mc_mmxext;
311 extern mpeg2_mc_t mpeg2_mc_3dnow;
312 extern mpeg2_mc_t mpeg2_mc_altivec;
313 extern mpeg2_mc_t mpeg2_mc_alpha;
314 extern mpeg2_mc_t mpeg2_mc_vis;
315 extern mpeg2_mc_t mpeg2_mc_arm;
316 
317 #endif /* LIBMPEG2_MPEG2_INTERNAL_H */
318