1 /*****************************************************************************
2 * video_parser.h : video parser thread
3 *****************************************************************************
4 * Copyright (C) 1999, 2000 VideoLAN
5 * $Id: video_parser.h,v 1.11 2001/07/18 14:21:00 massiot Exp $
7 * Authors: Christophe Massiot <massiot@via.ecp.fr>
8 * Jean-Marc Dressler <polux@via.ecp.fr>
9 * Stéphane Borel <stef@via.ecp.fr>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
24 *****************************************************************************/
27 * Block parsing structures
30 /*****************************************************************************
31 * macroblock_parsing_t : macroblock context & predictors
32 *****************************************************************************/
35 unsigned char i_quantizer_scale; /* scale of the quantization
37 int pi_dc_dct_pred[3]; /* ISO/IEC 13818-2 7.2.1 */
38 int pppi_pmv[2][2][2]; /* Motion vect predictors, 7.6.3 */
39 int i_motion_dir;/* Used for the next skipped macroblock */
41 /* Context used to optimize block parsing */
42 int i_motion_type, i_mv_count, i_mv_format;
43 boolean_t b_dmv, b_dct_type;
45 /* Coordinates of the upper-left pixel of the macroblock, in lum and
47 int i_l_x, i_l_y, i_c_x, i_c_y;
48 } macroblock_parsing_t;
50 /*****************************************************************************
51 * lookup_t : entry type for lookup tables *
52 *****************************************************************************/
53 typedef struct lookup_s
59 /*****************************************************************************
60 * ac_lookup_t : special entry type for lookup tables about ac coefficients
61 *****************************************************************************/
62 typedef struct dct_lookup_s
69 /*****************************************************************************
71 *****************************************************************************/
73 /* Macroblock Address Increment types */
74 #define MB_ADDRINC_ESCAPE 8
75 #define MB_ADDRINC_STUFFING 15
77 /* Error constant for lookup tables */
84 /* Constant for block decoding */
88 /*****************************************************************************
90 *****************************************************************************/
91 extern u8 pi_default_intra_quant[64];
92 extern u8 pi_default_nonintra_quant[64];
93 extern u8 pi_scan[2][64];
95 /*****************************************************************************
97 *****************************************************************************/
98 void vpar_InitCrop( struct vpar_thread_s* p_vpar );
99 void vpar_InitMbAddrInc( struct vpar_thread_s * p_vpar );
100 void vpar_InitPMBType( struct vpar_thread_s * p_vpar );
101 void vpar_InitBMBType( struct vpar_thread_s * p_vpar );
102 void vpar_InitCodedPattern( struct vpar_thread_s * p_vpar );
103 void vpar_InitDCTTables( struct vpar_thread_s * p_vpar );
104 void vpar_InitScanTable( struct vpar_thread_s * p_vpar );
106 typedef void (*f_picture_data_t)( struct vpar_thread_s * p_vpar,
108 #define PROTO_PICD( FUNCNAME ) \
109 void FUNCNAME( struct vpar_thread_s * p_vpar, int i_mb_base );
111 PROTO_PICD( vpar_PictureDataGENERIC )
112 #if (VPAR_OPTIM_LEVEL > 0)
113 PROTO_PICD( vpar_PictureData1I )
114 PROTO_PICD( vpar_PictureData1P )
115 PROTO_PICD( vpar_PictureData1B )
116 PROTO_PICD( vpar_PictureData1D )
117 PROTO_PICD( vpar_PictureData2IF )
118 PROTO_PICD( vpar_PictureData2PF )
119 PROTO_PICD( vpar_PictureData2BF )
121 #if (VPAR_OPTIM_LEVEL > 1)
122 PROTO_PICD( vpar_PictureData2IT )
123 PROTO_PICD( vpar_PictureData2PT )
124 PROTO_PICD( vpar_PictureData2BT )
125 PROTO_PICD( vpar_PictureData2IB )
126 PROTO_PICD( vpar_PictureData2PB )
127 PROTO_PICD( vpar_PictureData2BB )
132 * Headers parsing structures
135 /*****************************************************************************
136 * quant_matrix_t : Quantization Matrix
137 *****************************************************************************/
138 typedef struct quant_matrix_s
141 boolean_t b_allocated;
142 /* Has the matrix been allocated by vpar_headers ? */
145 /*****************************************************************************
146 * sequence_t : sequence descriptor
147 *****************************************************************************
148 * This structure should only be changed when reading the sequence header,
149 * or exceptionnally some extension structures (like quant_matrix).
150 *****************************************************************************/
151 typedef struct sequence_s
153 u32 i_height, i_width; /* height and width of the lum
154 * comp of the picture */
155 u32 i_size; /* total number of pel of the lum comp */
156 u32 i_mb_height, i_mb_width, i_mb_size;
157 /* the same, in macroblock units */
158 unsigned int i_aspect_ratio; /* height/width display ratio */
159 unsigned int i_matrix_coefficients;/* coeffs of the YUV transform */
160 int i_frame_rate; /* theoritical frame rate in fps*1001 */
161 boolean_t b_mpeg2; /* guess */
162 boolean_t b_progressive; /* progressive (ie.
163 * non-interlaced) frame */
164 unsigned int i_scalable_mode; /* scalability ; unsupported, but
165 * modifies the syntax of the binary
167 quant_matrix_t intra_quant, nonintra_quant;
168 quant_matrix_t chroma_intra_quant, chroma_nonintra_quant;
169 /* current quantization matrices */
171 /* Chromatic information */
172 unsigned int i_chroma_format; /* see CHROMA_* below */
173 int i_chroma_nb_blocks; /* number of chroma blocks */
174 u32 i_chroma_width;/* width of a line of the chroma comp */
175 u32 i_chroma_mb_width, i_chroma_mb_height;
176 /* size of a macroblock in the chroma buffer
177 * (eg. 8x8 or 8x16 or 16x16) */
180 picture_t * p_forward; /* current forward reference frame */
181 picture_t * p_backward; /* current backward reference frame */
182 mtime_t next_pts, next_dts;
184 boolean_t b_expect_discontinuity; /* reset the frame predictors
185 * after the current frame */
187 /* Copyright extension */
188 boolean_t b_copyright_flag; /* Whether the following
189 information is significant
192 boolean_t b_original;
196 /*****************************************************************************
197 * picture_parsing_t : parser context descriptor
198 *****************************************************************************
199 * This structure should only be changed when reading the picture header.
200 *****************************************************************************/
201 typedef struct picture_parsing_s
203 /* ISO/CEI 11172-2 backward compatibility */
204 boolean_t pb_full_pel_vector[2];
205 int i_forward_f_code, i_backward_f_code;
207 /* Values from the picture_coding_extension. Please refer to ISO/IEC
209 int ppi_f_code[2][2];
210 int i_intra_dc_precision;
211 boolean_t b_frame_pred_frame_dct, b_q_scale_type;
212 boolean_t b_intra_vlc_format;
213 boolean_t b_alternate_scan, b_progressive;
214 boolean_t b_top_field_first, b_concealment_mv;
215 boolean_t b_repeat_first_field;
216 /* Relative to the current field */
217 int i_coding_type, i_structure;
218 boolean_t b_frame_structure; /* i_structure == FRAME_STRUCTURE */
220 picture_t * p_picture; /* picture buffer from vout */
221 int i_current_structure; /* current parsed structure of
222 * p_picture (second field ?) */
223 boolean_t b_error; /* parsing error, try to recover */
225 int i_l_stride, i_c_stride;
226 /* number of coeffs to jump when changing
227 * lines (different with field pictures) */
230 /*****************************************************************************
232 *****************************************************************************/
233 #define PICTURE_START_CODE 0x100L
234 #define SLICE_START_CODE_MIN 0x101L
235 #define SLICE_START_CODE_MAX 0x1AFL
236 #define USER_DATA_START_CODE 0x1B2L
237 #define SEQUENCE_HEADER_CODE 0x1B3L
238 #define SEQUENCE_ERROR_CODE 0x1B4L
239 #define EXTENSION_START_CODE 0x1B5L
240 #define SEQUENCE_END_CODE 0x1B7L
241 #define GROUP_START_CODE 0x1B8L
243 /* extension start code IDs */
244 #define SEQUENCE_EXTENSION_ID 1
245 #define SEQUENCE_DISPLAY_EXTENSION_ID 2
246 #define QUANT_MATRIX_EXTENSION_ID 3
247 #define COPYRIGHT_EXTENSION_ID 4
248 #define SEQUENCE_SCALABLE_EXTENSION_ID 5
249 #define PICTURE_DISPLAY_EXTENSION_ID 7
250 #define PICTURE_CODING_EXTENSION_ID 8
251 #define PICTURE_SPATIAL_SCALABLE_EXTENSION_ID 9
252 #define PICTURE_TEMPORAL_SCALABLE_EXTENSION_ID 10
267 #define I_CODING_TYPE 1
268 #define P_CODING_TYPE 2
269 #define B_CODING_TYPE 3
270 #define D_CODING_TYPE 4 /* MPEG-1 ONLY */
271 /* other values are reserved */
273 /*****************************************************************************
275 *****************************************************************************/
276 int vpar_NextSequenceHeader( struct vpar_thread_s * p_vpar );
277 int vpar_ParseHeader( struct vpar_thread_s * p_vpar );
281 * Synchronization management
284 /*****************************************************************************
285 * video_synchro_t : timers for the video synchro
286 *****************************************************************************/
287 #define MAX_PIC_AVERAGE 8
289 /* Read the discussion on top of vpar_synchro.c for more information. */
290 typedef struct video_synchro_s
292 /* synchro algorithm */
295 /* date of the beginning of the decoding of the current picture */
296 mtime_t decoding_start;
298 /* stream properties */
299 unsigned int i_n_p, i_n_b;
301 /* decoding values */
302 mtime_t p_tau[4]; /* average decoding durations */
303 unsigned int pi_meaningful[4]; /* number of durations read */
304 /* and p_vout->render_time (read with p_vout->change_lock) */
307 unsigned int i_eta_p, i_eta_b;
308 boolean_t b_dropped_last; /* for special synchros below */
309 mtime_t backward_pts, current_pts;
310 int i_current_period; /* period to add to the next picture */
311 int i_backward_period; /* period to add after the next
313 * (backward_period * period / 2) */
316 unsigned int i_trashed_pic, i_not_chosen_pic, i_pic;
320 /* Synchro algorithms */
321 #define VPAR_SYNCHRO_DEFAULT 0
322 #define VPAR_SYNCHRO_I 1
323 #define VPAR_SYNCHRO_Iplus 2
324 #define VPAR_SYNCHRO_IP 3
325 #define VPAR_SYNCHRO_IPplus 4
326 #define VPAR_SYNCHRO_IPB 5
328 /*****************************************************************************
330 *****************************************************************************/
331 void vpar_SynchroInit ( struct vpar_thread_s * p_vpar );
332 boolean_t vpar_SynchroChoose ( struct vpar_thread_s * p_vpar,
333 int i_coding_type, int i_structure );
334 void vpar_SynchroTrash ( struct vpar_thread_s * p_vpar,
335 int i_coding_type, int i_structure );
336 void vpar_SynchroDecode ( struct vpar_thread_s * p_vpar,
337 int i_coding_type, int i_structure );
338 void vpar_SynchroEnd ( struct vpar_thread_s * p_vpar,
339 int i_coding_type, int i_structure,
341 mtime_t vpar_SynchroDate ( struct vpar_thread_s * p_vpar );
342 void vpar_SynchroNewPicture( struct vpar_thread_s * p_vpar, int i_coding_type,
343 int i_repeat_field );
347 * Video parser structures
350 /*****************************************************************************
351 * vpar_thread_t: video parser thread descriptor
352 *****************************************************************************/
353 typedef struct vpar_thread_s
355 bit_stream_t bit_stream;
357 /* Thread properties and locks */
358 vlc_thread_t thread_id; /* id for thread functions */
360 /* Input properties */
361 decoder_fifo_t * p_fifo; /* PES input fifo */
362 vdec_config_t * p_config;
364 /* Output properties */
365 vout_thread_t * p_vout; /* video output thread */
367 /* Decoder properties */
370 /* Parser properties */
372 picture_parsing_t picture;
373 macroblock_parsing_t mb;
374 video_synchro_t synchro;
379 /* table for macroblock address increment */
380 lookup_t pl_mb_addr_inc[2048];
381 /* tables for macroblock types 0=P 1=B */
382 lookup_t ppl_mb_type[2][64];
383 /* table for coded_block_pattern */
384 lookup_t * pl_coded_pattern;
385 /* variable length codes for the structure dct_dc_size for intra blocks */
386 lookup_t * pppl_dct_dc_size[2][2];
387 /* Structure to store the tables B14 & B15 (ISO/IEC 13818-2 B.4) */
388 dct_lookup_t ppl_dct_coef[2][16384];
391 /* Default quantization matrices */
392 u8 pi_default_intra_quant[64];
393 u8 pi_default_nonintra_quant[64];
395 /* Motion compensation plug-in used and shortcuts */
396 struct module_s * p_motion_module;
397 void ( * pppf_motion[4][2][4] ) ( struct macroblock_s * );
398 void ( * ppf_motion_skipped[4][4] ) ( struct macroblock_s * );
400 /* IDCT plugin used and shortcuts */
401 struct module_s * p_idct_module;
402 void ( * pf_sparse_idct ) ( void *, dctelem_t*, int );
403 void ( * pf_idct ) ( void *, dctelem_t*, int );
404 void ( * pf_norm_scan ) ( u8 ppi_scan[2][64] );
405 void ( * pf_decode_mb_c ) ( struct vdec_thread_s *, struct macroblock_s * );
406 void ( * pf_decode_mb_bw )( struct vdec_thread_s *, struct macroblock_s * );
410 count_t c_loops; /* number of loops */
411 count_t c_sequences; /* number of sequences */
412 count_t pc_pictures[4]; /* number of (coding_type) pictures read */
413 count_t pc_decoded_pictures[4]; /* number of (coding_type)
414 * pictures decoded */
415 count_t pc_malformed_pictures[4]; /* number of pictures trashed
420 /*****************************************************************************
422 *****************************************************************************/
424 /* Thread management functions - temporary ! */
425 vlc_thread_t vpar_CreateThread ( vdec_config_t * );
427 /*****************************************************************************
428 * NextStartCode : Find the next start code
429 *****************************************************************************/
430 static __inline__ void NextStartCode( bit_stream_t * p_bit_stream )
432 /* Re-align the buffer on an 8-bit boundary */
433 RealignBits( p_bit_stream );
435 while( ShowBits( p_bit_stream, 24 ) != 0x01L
436 && !p_bit_stream->p_decoder_fifo->b_die )
438 RemoveBits( p_bit_stream, 8 );
442 /*****************************************************************************
444 *****************************************************************************
445 * Quantizer scale factor (ISO/IEC 13818-2 7.4.2.2)
446 *****************************************************************************/
447 static __inline__ void LoadQuantizerScale( struct vpar_thread_s * p_vpar )
449 /* Quantization coefficient table */
450 static u8 ppi_quantizer_scale[3][32] =
456 0,2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,
457 32,34,36,38,40,42,44,46,48,50,52,54,56,58,60,62
461 0, 1, 2, 3, 4, 5, 6, 7, 8, 10,12,14,16,18,20, 22,
462 24,28,32,36,40,44,48,52,56,64,72,80,88,96,104,112
466 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
467 16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31
471 p_vpar->mb.i_quantizer_scale = ppi_quantizer_scale
472 [(!p_vpar->sequence.b_mpeg2 << 1) | p_vpar->picture.b_q_scale_type]
473 [GetBits( &p_vpar->bit_stream, 5 )];